开发者

how should I structure data for a complex object in ActionScript 3?

开发者 https://www.devze.com 2022-12-15 17:28 出处:网络
I hope this isn\'t too vague, but I\'m stuck on a problem that has put me in an Unfortunate Position.

I hope this isn't too vague, but I'm stuck on a problem that has put me in an Unfortunate Position.

I'm Flash developer getting my feet wet with with AS3 and am trying to build an interior decoration tool for a client. My thinking so far has been: create the basic user interface, get the screen flow down, and then finally use a couple of simple arrays to store user selections and stuff like that.

Naturally my 'couple of simple arrays' is totally inadequate to model the many user decisions that my program has to take into account. So I find myself trying to create an enormous, multi-dimensional array with several layers of nesting and before Panic sets in.

Here's an example of my thinking for the 'bedding' component of the application in pseudo ActionScript:

bedding['size']   = 'king':String
bedding['cover']  = cover:Array

    cover['type']           = 'coverlet':String
    cover['style']          = 'style_one':String
    cover['variation']      = 'varation_one':String
    cover['fabric']         = fabrics:Array

        fabrics[0] = 'paisely':String
        fabrics[1] = 'argyle':String
        fabrics[2] = 'plaid':String

    cover['trim'] 开发者_如何学Python         = trim:Array

        trims[0] = trim_pair:Array

            trim_pair['type']  = 'trim_one':String
            trim_pair['color'] = 'blue':String

        trims[1] = trim_pair:String
        trims[2] = trim_pair:String

    cover['embellishments'] = embellishment_pair:Array
        embellishment_pair['type'] = 'monogram':String
        embellishment_pair['letters'] = 'TL':String

... keep in mind that this is just a fraction of what goes into bedding and there are several other of these kinds of arrays that would go into a room like flooring and walls and funture... all equally complex. And I'll need to frequently access different combinations like, how many options under bedding have no value associated and things like that.

So, I realize I'm out of my league and am going to get hurt on this, but I'd like to try to get this right so that I get better and any help you guys can provide is great.

My questions are:

1) Could it be that using nested arrays like this actually isn't such a bad thing and I should just stick it out? That would suprise me, but I want to make sure I'm not already on the right path.

2) If not, where do I go from here if I want to do this right?

Off the top of my head I feel like I could maybe make everything class based. So my sheets are a class and beds have instances of sheets and rooms have instances of beds... etc. It think it would be complicated but might be the way to go.

Or maybe, I go the XML route and store all of the room options in nested blank XML nodes that a user then populates as they move through the application.

These are my thoughts but I'd like to hear what more experienced members of the community say.

Thank you so much for your help!


My suggestion would be to use a strongly typed model. Look into using collections and value objects to store and retrieve data. A collection could be a class that wraps an Array and provides a clean interface for fetching the value objects that it stores. Value objects are simple objects representing data that can be assembled in various ways to create more complex collections. Value objects can also be passed around to transfer data to various parts of an application. The advantage to using collections and value objects is that your code will be ( potentially ) more explicit and over-all easier to read than if you went with a dynamic approach. For some, the downside to this approach is that you end up with too many classes. Personally, I prefer working with many small to medium size classes versus one monolithic class.

If you are not familiar with the concept of value objects: http://en.wikipedia.org/wiki/Data_transfer_object.


AFAIK, AS3 is not well suited to the type of complex data model you're trying to create.

You need to completely decouple the UI/Flash tier from your "inventory" system. The UI should be completely abstract, with no knowledge of, or coupling to, your data schema or content. This could be accomplished with a middle-tier webservice-styled system that handles all the business logic around searching/retrieving/updating your data.

Store everything your UI needs to handle presentation-side rendering in your product metadata. This will allow you to add new products and types without having to update the UI every time new products are introduced. For example, if a product comes with an image, store a URI to the image with the product record and load it on demand. You could extend this all the way to custom animations, I believe- just reference an outside .SWF file and load it into your application on request.

0

精彩评论

暂无评论...
验证码 换一张
取 消