Friday, May 20, 2011

Factory Method Pattern

Definition

Provides an abstraction or an interface and lets subclass or implementing classes decide which class or method should be instantiated or called, based on the conditions or parameters given.

Explanation

Assume that you have a set of classes which extends a common super class or interface. Now you will create a concrete class with a method which accepts one or more arguments. This method is our factory method. What it does is, based on the arguments passed factory method does logical operations and decides on which sub class to instantiate. This factory method will have the super class as its return type. So that, you can program for the interface and not for the implementation. This is all about factory method design pattern.

Example

Assume that you have a set of classes which extends a common super class or interface. Now you will create a concrete class with a method which accepts one or more arguments. This method is our factory method. What it does is, based on the arguments passed factory method does logical operations and decides on which sub class to instantiate. This factory method will have the super class as its return type. So that, you can program for the interface and not for the implementation. This is all about factory method design pattern.

When to use a Factory Pattern?

  • The Factory patterns can be used in following cases:
    When a class does not know which class of objects it must create.
  • A class specifies its sub-classes to specify which objects to create.
  • In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.

UML of Factory-method Pattern

The UML class diagram above describes an implementation of the factory method design pattern.
Factory Method Pattern
Factory Method Pattern UML


Participants in Factory-method pattern

In the diagram above, there are four classes:
  • IFactory: This is an abstract base class or interface for the concrete factory classes that will actually generate new objects.
  • ConcreateFactory: Inheriting from the FactoryBase class, the concreate factory classes inherit the actual factory method. This is overridden with the object generation code unless already implemented in full in the base class.
  • IProduct: This abstract class is the base class or interface for the types of object that the factory can create. It is also the return type for the factory method. Again, this can be a simple interface info general functionality is to be inherited by its subclasses.
  • ConcreateProduct: Multiple subclasses of the Product class are defined, each containing specific functionality. Object of these classes are generated by the factory method.

Example Code in java

Factory and its implementation
interface  IFactory
{
public Product factoryMethod(int type);
}

public class ConcreteFactory implements IFactory
{
@Override
public Product factoryMethod(int type)
{
switch (type)
{
case 1:
return new ConcreteProduct1();

case 2:
return new ConcreteProduct2();

default:
throw new ArgumentException("Invalid type.", "type");
}
}
}
Product class
interface class  IProduct { }

public class ConcreteProduct1 implements IProduct { }

public class ConcreteProduct2 implements IProduct { }
Testing the program
public class  FactoryMethodDemo
{
public static void main(String[] args)
{
IFactory myFactory = new ConcreteFactory();
ConcreteProduct1 product1;
//create product1 from factory
product1 = myFactory.factoryMethod(1); 
ConcreteProduct2 product2;
//create product2 from factory
product2 = myFactory2.factoryMethod(2);
}
}

Advantage of Factory-method pattern

  • Eliminates the need to bind application-specific classes into your code
  • Provides hooks for subclassing. 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 heirarchies. Factory method localises knowledge of which classes belong together. Parallel class heirarchies result when a class delegates some of its responsibilities to a separate class.

Disadvantage of Factory-method pattern


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

No comments:

Post a Comment

Chitika