Factory Method

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (How it works (Structure))
m (Participants)
Line 29: Line 29:
 
* Product: this defines the interface of objects the factory method creates.
 
* Product: this defines the interface of objects the factory method creates.
 
* ConcreteProduct: this implements the Product interface.
 
* ConcreteProduct: this implements the Product interface.
* Creator: this class declares the factory method, which will return an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object but this really depends on what behaviour the developer requires. Creator, although abstract may have a method like the anOperation() method mentioned above, that simply calls the factoryMethod() to create a Product object[1].  
+
* Creator: this class declares the factory method, which will return an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object but this really depends on what behaviour the developer requires. Creator, although abstract may have a method like the anOperation() method mentioned above, that simply calls the factoryMethod() to create a Product object.  
 
* ConcreteCreator: this class overrides the factoryMethod() to return an instance of ConcreteProduct.
 
* ConcreteCreator: this class overrides the factoryMethod() to return an instance of ConcreteProduct.

Revision as of 06:11, 26 September 2008

(This is summarised from GoF design patterns book)

Contents

Intent

The factory method is useful when you wish to define an interface for creating an object, but you want to let subclasses decide which class to create. Factory Method lets an abstract/normal class defer instantiation of an object of a different class to subclasses of the abstract/normal class.

When to use it (Applicability)

Useful when:

  • A class cannot anticipate the class of an object it must create.
  • A class wants its subclasses to specify the objects it creates.
  • Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.


How it works (Structure)

Factory Method.jpg

In the above diagram the anOperation() method has some code like:

* product = FactoryMethod();

and the concrete implementation of the factoryMethod() in the ConcreteCreator class would have some code like:

* return new ConcreteProduct;

Participants

  • Product: this defines the interface of objects the factory method creates.
  • ConcreteProduct: this implements the Product interface.
  • Creator: this class declares the factory method, which will return an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object but this really depends on what behaviour the developer requires. Creator, although abstract may have a method like the anOperation() method mentioned above, that simply calls the factoryMethod() to create a Product object.
  • ConcreteCreator: this class overrides the factoryMethod() to return an instance of ConcreteProduct.
Personal tools