Method Factory Pattern


Motivation

Also known as Virtual Constructor, the Factory Method is related to the idea on which libraries work: a library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.

The Factory method works just the same way: it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you’ve paid for the room you want and this way he can be looked at as a �room� factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a �phone-call� factory, because he controls the access to calls, too.

d1

Intent

  • Defines an interface for creating objects, but let subclasses to decide which class to instantiate
  • Refers to the newly created object through a common interface

Example PizzaStore in ebook Head First DesignPattern

d1

Note about Method Factory Pattern.

A factory method handles object creation and encapsulates it in a subclass. This decouples the client code in the superclass from the object creation code in the subclass.

abstract Product factoryMethod(String type)

  1. A factory method is abstract so the subclasses are counted on to handle object creation.
  2. A factory method returns a Product that is typically used within methods defined in the superClass.
  3. A factory method isolates the client (the code in the super class, like orderPizza() ) from knowing what kind of concrete Poduct is actually created.
  4. A factory method may be parameterized (or not) to select among several vatiations of a product.

Finally time with Factory Method Pattern

All factory patterns encapsulate object creation. The Factory method pattern encapsulates object creation by letting subclasses decide what objects to create. Let’s check out these class diagrams to see who the player are in this pattern.

d1

d1

Applicability & Examples

The need for implementing the Factory Method is very frequent. The cases are the ones below:

  • when a class can’t anticipate the type of the objects it is supposed to create
  • when a class wants its subclasses to be the ones to specific the type of a newly created object

Specific problems and implementation

When implementing the Factory Method design pattern some issues may appear:

Definition of Creator class

If we apply the pattern to an already written code there may be problems with the way we have the Creator class already defined. There are two cases:

  • Creator class is abstract and generating method does not have any implementation. In this case the ConcreteCreator classes must define their own generation method and this situation usually appears in the cases where the Creator class can’t foresee what ConcreteProduct it will instantiate.
  • Creator class is a concrete class, the generating method having a default implementation. If this happens, the ConcreteCreator classes will use the generating method for flexibility rather than for generation. The programmer will always be able to modify the class of the objects that the Creator class implicitly creates, redefining the generation method.

Factory method is just a particular case of the factory design pattern. In the same time it is the most known factory pattern, maybe because it was published in the GoF. In modern programming languages the factory with registration is more used.

Drawbacks and Benefits

Here are the benefits and drawbacks of factory method pattern:

  • + The main reason for which the factory pattern is used is that it introduces a separation between the application and a family of classes (it introduces weak coupling instead of tight coupling hiding concrete classes from the application). It provides a simple way of extending the family of products with minor changes in application code.
  • + It provides customization hooks. When the objects are created directly inside the class it’s hard to replace them by objects which extend their functionality. If a factory is used instead to create a family of objects the customized objects can easily replace the original objects, configuring the factory to create them.
  • – The factory has to be used for a family of objects. If the classes doesn’t extend common base class or interface they can not be used in a factory design template.

Hot Points:

The factory method is one of the most used and one of the more robust design patterns. There are only few points which have to be considered when you implement a factory method.

When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory. I you’re code should have a lot of code like the following, reconsider it.

if (genericProduct typeof ConcreteProduct)
	((ConcreteProduct)genericProduct).doSomeConcreteOperation();

Nguồn tham khảo : OODesign.com and Ebook Head First Design Pattern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: