On 5/1/07, pedr browne <[EMAIL PROTECTED]> wrote:
Hi,

A have a Factory that needs to instanciate Objects with differing numbers of
parameters (1-4). Is there any way to deal with this situation and maintain
strong typing.

private function createItem(param_ob:Object, key:String):Void{
   switch(key){
      case "item_1":
         myItem = new Item_1(param_ob.width, param_ob.y);
         break
     case "item2":
         myItem = new Item_2(param_ob.height, param_ob.color);
         break
     etc...
   }
}

(Why is the factory method private?)

One way is to split it into two functions:
public function static createFromWidthY(width:Number, y:Number):Item {
   return new Item1(width, y);
}
public function static createFromHeightColor(height:Number, color:Number):Item {
   return new Item2(height, color);
}

(Or call them createItem1 and createItem2 or whatever.)

Another way is to use classes for parameter types:

// Make an "abstract" class for parameters:

class myPackage.ItemParams extends Object {
   private function ItemParams() {
       super();
   }
}

// Then make "concrete" subclasses for specific types of parameters:

class myPackage.WidthYParams extends ItemParams {
   public function WidthYParams(width:Number, y:Number) {
       super();
       this.width = width;
       this.y = y;
   }
   public function get width():Number {
       return _width;
   }
   public function set width(value:Number):Void {
       if (isFinite(value)) {
           _width = value;
       }
   }
   public function get y():Number {
       return _y;
   }
   public function set y(value:Number):Void {
       if (isFinite(value)) {
           _y = value;
       }
   }
   private var _width:Number = 0;
   private var _y:Number = 0;
}

class myPackage.HeightColorParams extends ItemParams {
   public function HeightColorParams (height:Number, color:Number) {
       super();
       this.height= height;
       this.color = color;
   }
   public function get color():Number {
       return _color;
   }
   public function set color(value:Number):Void {
       if (isFinite(value)) {
           _color = Math.min(0xFFFFFF, Math.max(0, Math.floor(value)));
       }
   }
   public function get height():Number {
       return _height;
   }
   public function set height(value:Number):Void {
       if (isFinite(value)) {
           _height = value;
       }
   }
   private var _color:Number = 0;
   private var _height:Number = 0;
}

// Then, in your factory method:

public static function createItem(params:ItemParams):Item {
   if (!(params instanceof ItemParams)) {
       throw new Error("Invalid argument in call to
MyFactory.createItem: " + params);
   }
   if (params instanceof WidthYParams) {
       return new Item1(WidthYParams(params).width, WidthYParams(params).y);
   }
   if (params instanceof HeightColorParams) {
       return new Item2(HeightColorParams(params).height,
                                HeightColorParams(params).color);
   }
   throw new Error("Unrecognized item parameters: " + params);
}

--
T. Michael Keesey
Director of Technology
Exopolis, Inc.
2894 Rowena Avenue Ste. B
Los Angeles, California 90039
--
The Dinosauricon: http://dino.lm.com
Parry & Carney: http://parryandcarney.com
ISPN Forum: http://www.phylonames.org/forum/
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to