Search code examples
actionscript-3superclass

AS3 Super Class Issue


I have a problem and I'm not too sure the best way to resolve it.

Scenario:

I have a Super Class called 'food' and I have 20 different foods and extends 'food' like Pizza, Curry, Fish and Chip etc.

When I remove a 'food' I keep a record of it so I can reuse (for performance purposes). Can I make a new Pizza class that uses an old 'food'?

E.g.

public class Pizza extends food
{
      public function Pizza()
      {
           super = FOOD.returnUsedFoodClass();
      }
}

Is this possible or would I need to save the extending Class as well?

Hope this all make sense.

EDIT:

When I say remove I mean I no longer need it - so I would normally remove all references to it. But instead, I have placed all 'food' classes that I no longer need in a static vector so I can reuse them later.


Solution

  • You misunderstand the basic OOP principles here.

    First: a constructor runs only once and only when the object is created so any attempt to stop the creation of the object or replace the object from within its constructor is illogical and cannot succeed since the object at that moment is already created.

    Second: Classic misunderstanding of the super keyword. No super doesn't point to any other instance of any other object, super in constructor points to the super class implementation of the constructor. Trying to assign an object to super cannot work and is also illogical. I'm guessing you meant to use 'this' which would also not work anyway.

    What you are trying to achieve cannot be done that way and this in any OOP language. There's no way to run a constructor (meaning creating the object) and make this object point to something else within its own constructor. What you are looking for is a classic object pooling system via static methods like this:

    var pizza:Pizza = Food.getFood("pizza") as Pizza;
    

    Where the static method checks if any Pizza instance (from the pool) is available and if it is it returns it and if it's not it creates a new one and returns it.

    Pooling can be implemented loosely or explicitly, I prefer the more solid and flexible explicit version. Here's an example:

    Food class pooling additions:

    static private var recycledInstances:Vector.<Food> = new Vector.<Food>();
    //hold our reclycled instances
    
    public function recycle():void
    {
        var index:int = recycledInstances.indexOf(this);
        if(index >= 0)
        {
            return;
        }
        recycledInstances.push(this);
    }
    //explicitly add this instance to recycle
    
    private function reset():void
    {
    
    }
    //to run in constructor and when the instance is retreived from recycle
    //this method purpose is to reset all values to default.
    

    Now when an instance is no longer used you call the instance recycle() method to place it in recycle. Then when you need a new instance you do:

    var food:Food = Food.getFood();
    

    And this is implemented that way in Food class:

    static public function getFood():Food
    {
        if(recycledInstances.length)
        {
             var totalInstances:uint = recycledInstances.length;
             var instance:Food = recycledInstances[totalInstances - 1];
             instance.reset();
             recycledInstances.length -= 1;//faster than splice
             return instance;
        }
        return new Food();
    }
    

    You can extend this easily to descendant of food class by adding a type member variable to Food for example and check the type of recycled instances before returning them.