Sunday, 6 November 2011

Exploring the Factory Design Pattern

Exploring the Factory Design Pattern

Name : Factory Design Pattern

Context:

The factory method pattern is an object-oriented design pattern to implement the concept of factories. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created .The factory method design pattern handles the problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.

Some of the processes required in the creation of an object include determining which object to create, managing the lifetime of the object, and managing specialized build-up and tear-down concerns of the object. Outside the scope of design patterns, the term factory method can also refer to a method of a factory whose main purpose is creation of objects.

The Problem:

One of the goals of object-oriented design is to delegate responsibility among different objects. This kind of partitioning is good since it encourages Encapsulation and Delegation.

  • Sometimes, an Application (or framework) at runtime, cannot anticipate the class of object that it must create. The Application (or framework) may know that it has to instantiate classes, but it may only know about abstract classes (or interfaces), which it cannot instantiate. Thus the Application class may only know when it has to instantiate a new Object of a class, not what kind of subclass to create.
  • A class may want it's subclasses to specify the objects to be created.
  • A class may delegate responsibility to one of several helper subclasses so that knowledge can be localized to specific helper subclasses.

The Solution:

Factory Method is a creational pattern. This pattern helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate. We call this a Factory Pattern since it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. The Factory Pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code.

Factories have a simple function: Churn out objects.

Obviously, a factory is not needed to make an object. A simple call to new will do it for us. However, the use of factories gives the programmer the opportunity to abstract the specific attributes of an Object into specific subclasses which create them.


Structure:

The Factory Pattern is all about "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses" Thus, as defined by Gamma et al, "The Factory Method lets a class defer instantiation to subclasses".

Figure 1 below illustrates the roles of the Factory Pattern.

Figure 1: The roles of the Factory Pattern

As shown in the figure, the Factory Pattern has a couple of roles - a Creator and a Concrete Creator. This pattern is used when a class (the Creator) does not know beforehand all the subclasses that it will create. Instead, each subclass (the Concrete Creator) is left with the responsibility of creating the actual object instances.

Implementation:


The implementation is really simple

  • The client needs a product, but instead of creating it directly using the new operator, it asks the factory object for a new product, providing the information about the type of object it needs.
  • The factory instantiates a new concrete product and then returns to the client the newly created product (casted to abstract product class).
  • The client uses the products as abstract products without being aware about their concrete implementation.
Consequences:
Advantages:

  • Eliminates the need to bind application-specific classes into your code
  • Provides hooks for sub classing. Creating objects inside a class with a factory method is always more flexible than creating an object directly. This method gives subclasses a hook for providing an extended version of an object
  • Connects parallel hierarchies. Factory method localizes knowledge of which classes belong together. Parallel class hierarchies result when a class delegates some of its responsibilities to a separate class.
Disadvantages :

  • Clients might have to subclass the Creator class just to create a particular Concrete object.




Also See:
Abstract Factory, Template, and Prototype Patterns.


No comments:

Post a Comment