Saturday, July 21, 2012

Decorator Pattern in Action Script partII (Games)

Please check Decorator pattern part I.

In order we not overwhelm all decorators in extended classes (create less foot print) with the functions that aren't used in them(for example we have some decorator that doesn't allow magical ehancement, so what for would be function getFireResitance() subclassed from some class in hierarchy), we extend base from Proxy class.(Can be done with some other pattern inside this one also)

 
  public class ArmorDecorator extends Proxy implements IArmor {
    private var _armorToBeDecorated : * ;
    private var _type : int ;
    private var _container:Sprite;
   
   
    public function ArmorDecorator( decoratedArmor : * ){
      _armorToBeDecorated = decoratedArmor;
     
      _container = new Sprite();
     
      //add decoratedArmor to display list
      _armorToBeDecorated.getParent().addChild(this._container);
       }
      
       //if Decorator doesn't have functionality  required would ask wrapped decorator or component for it.
       override flash_proxy function getProperty(name:*):*
       {
             return _armorToBeDecorated[name];
       }
       //if Decorator doesn't have functionality  required would call from wrapped decorator or component .
       override flash_proxy function callProperty(name:*, ...rest):*
       {
          // trace("callProp", name,this[name]);
                  
           return _armorToBeDecorated[name].apply(null,rest);
       }


Source: Download (without visual component)

No comments:

Post a Comment