Factory Pattern

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.

Factory Pattern


I.  Factory Pattern: 

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.

A class may need it's subclasses to specify the objects to be created or delegate responsibility to one of several helper subclasses so that knowledge can be localized to specific helper subclasses.
Even Sometimes, an Application (or framework) at runtime, is not able to judge properly the class of an 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.

Creational design pattern , more specifically the Factory Pattern tries to  resolve out such issues. 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 to decide which class to instantiate. We call this a Factory Pattern since it is responsible for "Manufacturing" an Object. It helps to  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. It is an object oriented design pattern that returns an instance of one of the several possible classes based on the data passed to it. Generally all the classes that it returns have common methods and also have a common parent class. It should be noted that all the subclasses performs the different task and is optimized for different kind of data. 

A factory is not needed to make an object. A simple call to new will do it for you. However, the use of factories gives the programmer the opportunity to abstract the specific attributes of an object into specific subclasses which create them. 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" 

Benefits: Factory pattern allows the subclasses to choose the type of objects to create. One of the other benefit of factory pattern is that many of the methods created using the factory method technique do not depend on subclasses, this means that it is beneficial to define the methods using the factory method technique that are used to create objects to gain the other benefits. These methods are known as static methods. 

Usage: Classes that are parallel in hierarchies usually require objects of one hierarchy for creating appropriate objects from another. Factory methods are mostly used in frameworks and toolkits where library codes required to create the objects of specific types that may be subclassed by applications with the help of framework.

The Factory patterns can be used in following cases:
1. When a class does not know which class of objects it must create.
2. A class specifies its sub-classes to specify which objects to create.
3. 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.

Let?s try to understand this pattern with an example. 
Example: Let?s take an application that ask to enter the name and the Id of a person . If the person is the administrator then it displays welcome message saying Hello Mr. <Name> You are the administrator of the organization otherwise it displays a message saying Hello Mr <Name> you are not the administrator of the organization.

The skeleton of the code can be given here.

public class Employee {
public String name;
private String Id = "123542";
public String getName() {
return name;
public String getId() {
return Id;

This is a simple Employee class that contains the methods for name and Id. Now, we take two sub-classes, Administrator and NotAnAdministrator which will print a message on the screen accordingly.

public class Administrator extends Employee {
public Employee(String fullName) {
System.out.println("Hello Mr. "+ fullName + "You are the administrator of this organization");

Also, the Not an Administrator

public class NotAnAdministrator extends Person {
public NotanAdministrator(String fullNname) {
System.out.println("Hello Mr. " + fullNname + "you are not the administrator of this organization");

Now, we will create the Verification class which will return a message depending on the data provided.

public class Verification {
public static void main(String args[]) {
Verification verify = new Verification();
verify.getEmployee(args[0], args[1]);
public Employee getEmployee(String name, String Id) {
if ((name.equals("Zulfiqar")&&(Id.equals("123542")))
return new Administrator(name);
return new NotanAdministrator(name);

This class takes two arguments from the system at runtime and prints the names and a message accordingly.

Running the program:
After compilation, run the code with the arguments Zulfiqar and 123542:

java Zulfiqar 123542

The result returned is: ?Hello Mr. Zulfiqar You are the administrator of the organization?.

Share on Google+Share on Google+

Factory Pattern

Posted on: April 1, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Factory Pattern  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image