Hi,

Thanks a lot for all the replies. Most helpful. It's a funny situation. I'm
using a deserialised XML file to dictate the content and layout of each
page. A page might contain 1. A heading, 2. A TextField 3. A link or it
might contain 1.A heading 2. A thumbnailMenu or various other permutations.
I have written a class for each item that implements an IItem interface, and
they all have certain identical methods (setPosition(), setScheme(), close()
etc), but they also have unique props that need to be set (The label of the
header, the label / path of a link etc.) Also to comlicate things, if the
item is a menu, I need to attach an eventListener.

I like the idea of using classes to encapsulate the parameters, but where
would these be created? I guess it would make sense to do it at
deserialisation, but then that gives two places that will be subject to
change and that is a bad thing. I could do it in a separate method in the
factory I suppose.

Thanks

____________________________________________________________________________________

Message: 17
Date: Tue, 1 May 2007 09:30:32 -0700
From: "T. Michael Keesey" <[EMAIL PROTECTED]>
Subject: Re: [Flashcoders] factory with varying numbers of params
To: flashcoders@chattyfig.figleaf.com
Message-ID:
       <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=UTF-8; format=flowed

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