Factory Pattern

Factory Pattern:

Intro :

Lets see what Factory means in terms of software engineering :

Factory : A factory is an object which works for creating other objects according to various allocation schemes.
A factory object has a method for every kind of object it is capable of creating. The methods in the factory object accept the parameters which are basically tells which object to be created and these methods return object needed.

Factory design pattern is most widely used design pattern in modern application programming languages like java.
Lets see why there is need for factory design pattern :

Need :

As we seen previously in intro section, factory design pattern is used to create object without exposing the instantiation logic to the client.

Implementation :

1. All objects which gets created have one common interface which provides the contract when objects being created.
2. When client needs the object then it ask factory for that object instead of creating one.
3. Factory receives the parameter and then decides which object to create and then it creates the object and returns it.
4. The client receives the objects without actually looking up into the instantiation part.

Example :

Lets create interface Shape having method printShape(). Any shape our factory returns must implement Shape interface.

public interface Shape {

public void printShape();


Now we'll define a few concrete classes which implements our Shape interface. Each Class implements Shape interface and have printShape() method as per the properties of each Class.

public class Triangle implements Shape {

/** Creates a new instance of Triangle */
public Triangle() {
public void printShape(){
System.out.println("Triangle shape");

public class Rectangle implements Shape {

/** Creates a new instance of Rectangle */
public Rectangle() {
public void printShape(){
System.out.println("Rectangle shape");
public class Circle implements Shape {

/** Creates a new instance of Circle */
public Circle() {
public void printShape(){
System.out.println("Circle shape");

Java Factory Class :

Now time to create the Factory class which has static method getShape() which returns the Shape which we want to print (or use). Here we can note that when getShape() returns the Shape interface without knowing that object is of type triangle, rectangle or circle.

public class ShapeFactory {

/** Creates a new instance of ShapeFactory */
public ShapeFactory() {

public static Shape getShape( String shapeId ){
return new Circle();
}else if(shapeId.equals("rectangle")){
return new Rectangle();
}else if(shapeId.equals("triangle")){
return new Triangle();
return null;


Below is the Factory Simulator class where we use Factory class to obtain objects and print the shapes.

public class FactorySimulator {

/** Creates a new instance of FactorySimulator */
public FactorySimulator() {

public static void main(String[] args){

Shape shape = ShapeFactory.getShape("triangle");
shape = ShapeFactory.getShape("circle");
shape = ShapeFactory.getShape("rectangle");

Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


  1. I would add to number 4 that the client does not need any knowledge of the sublcasses, just the interface.

  2. Thanks David for more clearer version of point 4.