A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: OOP Question?

  1. #1
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185

    OOP Question?

    Hey everyone

    I have this one simple thing that I just can't figure out in all this Object-Oriented Actionscript 3.

    I can explain it like this:

    I have a FruitInfo class, that has an getFruitInfo method. This method is called with a string of a fruitname, and then it gets the fruitinfo from a Mysql/php backend.

    Let's say I wanted to know something about an apple. I call getInfo("apple"), and the method returns an AppleInfo object with a bunch of properties with info about the apple.

    Let's then say, that I called the method with "pineapple". Then the getInfo method would return an PineappleInfo object with a bunch of different properties describing the pineapple.

    If I have 40 fruits, I don't want to hardcode an if-statement in the FruitInfo class that says:


    Code:
    if(fruit == "apple")
    {
        // make an AppleInfo object
        // put values into properties
        // return it
    }
    elseif(fruit == "pineapple")
    {
        // make an PineappleInfo object
        // put values into properties
        // return it
    }
    ..so on...
    I want to be able only to make all the AppleInfo, PineappleInfo, ... classes with properties of the corresponding mysql columns, and the I want the FruitInfo class to dynamic put the right values into the right properties and return the right kind of info object.

    How do I do this, and will it make my code slower to make it dynamic?

    thanks - Rune
    --- Ronze ---

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you can make your arguments exactly match your classnames, you can do this:
    Code:
    var c:Class = getDefinitionByName(fruit);
    var f:Fruit = new c();
    f.initialize(values);
    And if you can't, you can make a map from argument to class with an associative Object/Array.

    Implied above is that Apple and Pineapple are actual classes that subclass Fruit, and implement /override an initialize method which applies a list of values to their properties.

  3. #3
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    Also worth noting that you cannot just create new classes on the fly in AS3; you actually need an .as document for each one of these fruits called Apple, Pear, etc.
    Seems to me the wise way to do this would be to create one class called Fruit and no subclasses at all; let Fruit be initialized with _all_ the possible vars that pertain to _every_ single fruit, then fill those things in with your mysql data, including the name of an associated image or Library graphic.
    Then in the class that's creating the Fruit instances, create an object called fruits{} and, when you create the instances, put them into fruits[type] ...e.g. fruits.apple; then you can tell everything in fruits.apple to perform a certain behaviour if you want to, without touching the other types of fruits, and without creating 40 different subclasses you probably don't need...

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You could certainly do that, and it would let you add new fruit types without recompiling the swf. But it's hardly in the spirit of OOP.

  5. #5
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    True that, 5Tons -- but I mean, having 40 classes extending Fruit is kinda ineffecient, especially if most of them share a lot of the same properties. I'm all for normalizing stuff as much as possible; but it makes sense to choose the greatest possible divisor when choosing how to structure code or a DB, rather than be redundant. True, Fruit could pass most of the properties and methods inherently -- but won't some unusual properties and methods be shared by more than one subclass? Then you have to decide how many subclasses having "bumpySkin" or "seedsOutside" or something should imply including that property in Fruit as a whole...
    If you wanna keep it in OOP-style, you could make the properties and behaviors be their own classes...that seems like a more rational way to divide it up than by name, which has to be the most inefficient way...in other words, think of the hierarchy of properties by which you're most likely to search out or use a particular instance, and structure accordingly...

  6. #6
    Ronze Ronze's Avatar
    Join Date
    Oct 2002
    Location
    Copenhagen, Denmark
    Posts
    185
    ok, thanks a lot :-)
    Im fiddling around with it, and will see what I find out.
    thanks again
    --- Ronze ---

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center