[Flashcoders] factory with varying numbers of params

2007-05-02 Thread pedr browne

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(0xFF, 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

RE: [Flashcoders] factory with varying numbers of params

2007-05-02 Thread Danny Kodicek
  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.

This sounds like what I'm doing at the moment. What I did was instead of
using an interface, to make all my elements inherit the same base Element
class, then when running a createElement function, I simply defined it as
returning an instance of Element. It sounds to me like you're giving in to a
common problem of letting the OO cart pull the design horse - you're trying
to force a function to be strongly typed when by design it's creating
elements of different types.

Danny


___
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


Re: [Flashcoders] factory with varying numbers of params

2007-05-02 Thread Ron Wheeler

Looks like a Decorator Pattern.

Danny Kodicek wrote:
  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.



This sounds like what I'm doing at the moment. What I did was instead of
using an interface, to make all my elements inherit the same base Element
class, then when running a createElement function, I simply defined it as
returning an instance of Element. It sounds to me like you're giving in to a
common problem of letting the OO cart pull the design horse - you're trying
to force a function to be strongly typed when by design it's creating
elements of different types.

Danny


___
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


  

___
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


[Flashcoders] factory with varying numbers of params

2007-05-01 Thread pedr browne

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...
  }
}
___
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


Re: [Flashcoders] factory with varying numbers of params

2007-05-01 Thread Listas
pedr, i would prefer to use the arguments array, instead of an object, 
but i´m afraid this isn´t strong typed as you wish


private function createItem(key:String):Void{

 switch(key){
case item_1:
   var wid_num:Number = arguments[1];

   var y_num:Number = arguments[2];
   myItem = new Item_1(wid_num, y_num);
   break
   case item2:
   var hei_num:Number = arguments[1];
   var color_num:Number = arguments[2];
   myItem = new Item_2(hei_num, color_num);
   break
   etc...
 }

}

Ruy Adorno

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...
  }
}
___
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






___
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


Re: [Flashcoders] factory with varying numbers of params

2007-05-01 Thread Andy Herrman

There are two ways I can think of to do this.  One would be to have
multiple factory functions, one for each type of object you want to
create.  The other would be to create a simple class (FactoryParams or
something like that) which stores all the parameters.  You pass that
as the parameter object, and you can have that parameter object be
strongly typed.

class FactoryParams {
 public var firstParam:String;
 public var secondParam:Number;
 public var thirdParam:SomeComplexObject
}

By using a class instead of a generic object you can keep the type
information (and if you use getters/setters instead of public
variables then you can even do validation on the values if you want).

  -Andy

On 5/1/07, Listas [EMAIL PROTECTED] wrote:

pedr, i would prefer to use the arguments array, instead of an object,
but i´m afraid this isn´t strong typed as you wish

private function createItem(key:String):Void{

  switch(key){
 case item_1:
var wid_num:Number = arguments[1];
var y_num:Number = arguments[2];
myItem = new Item_1(wid_num, y_num);
break
case item2:
var hei_num:Number = arguments[1];
var color_num:Number = arguments[2];
myItem = new Item_2(hei_num, color_num);
break
etc...
  }

}

Ruy Adorno
 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...
   }
 }
 ___
 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





___
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


___
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


Re: [Flashcoders] factory with varying numbers of params

2007-05-01 Thread T. Michael Keesey

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(0xFF, 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