I would say it's more flexible to use the ClassFactory because you can
dynamically set the properties in the ClassFactory.  Simple example:

 

Say you had an application that creates Squares - just simple rectangles
on the screen.  When the user presses a button, a new square pops up in
a random location.  If you used a hard-coded class to fill your Square
class with pre-set instance data, you're locked in to your
implementation.  You'll have to write a new class if you wanted to give
the user the option to create a RedSquare instead of a Square.  It gets
exponentially difficult if you have multiple things that might need to
change as well: say color, size, border-thickness, etc.  You'd have to
make a new class (and then configure your application to use that new
class!) for each property of Square.  Not pretty!

 

[Pseudocode]

var newSquare:Square;

If (userInput.color == "Red")

                square = new RedSquare();

else if (userInput.color == "Blue")

                square = new BlueSquare();

else if (userInput.border == "Thick" && userInput.color == "Blue")

                square = new BlueSquareWithThickEdge();         

etc...

etc...

[/Pseudocode]

 

With the ClassFactory, you could just update the
myClassFactory.properties variables:

 

[Pseudocode]

var properties:Object = new Object();

properties.color = userInput.color;

properties.edge = userInput.edge;

 

var myClassFactory:ClassFactory = new ClassFactory(Square);

myClassFactory.properties = properties;

var mySquare:Square = myClassFactory.newInstance();

[/Pseudocode]

 

It doesn't look a whole lot smaller now (it gets a lot better when
you're add a few dozen more else-ifs to the first attempt), but it's far
more elegant as well IMO.

 

Jonathon

 

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of valdhor
Sent: Friday, May 30, 2008 10:05 AM
To: [email protected]
Subject: [flexcoders] Re: What is a classfactory and why would I use
one?

 

Ok, that makes sense.

What is the benefit of going the classfactory way instead of extending
a class and setting all the properties in a class constructor?

_ 

Reply via email to