Wednesday, June 16, 2010

Factory Method Pattern

Today I had a discussion with one of my office colleagues about GOF(Gang of Four Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) patterns and advantages. During the discussion, he claimed a trivial question posed at him during the interview. He explained me the scenario.

A shelf should be able to hold any items like books, laptops etc. How to design such objects?
A shelf is an object which does not know what type of object it will hold. This gave the clue of implementing factory method pattern by GOF.

Use the Factory Method pattern when
1. a class can't anticipate the class of objects it must create.
2. a class wants its subclasses to specify the objects it creates.
3. classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

Shelf is an object which does not what type of object a book or a laptop is. Hence, based on the applicability of Factory Method as provided by GOF mentioned above, the below sample is illustrated.

import java.util.ArrayList;
class Shelf
ArrayList list=null;
ContextFactory factory=null;
public void storeObject(String object){
if(list==null && factory==null){
list=new ArrayList();
factory=new ContextFactory();
Item itemobject=factory.createObject(object);
public void printObjects(){

interface Items // Creator interface
public Item createObject(String object);

class ContextFactory implements Items // Creator class decides which object to be created.
public Item createObject(String object){

return new Book();
}else if(object.startsWith("Laptop")){

return new Laptop();

return null;
interface Item
// no implementation
class Book implements Item // class Book is of type Item
public Book(){

System.out.println("Book Object Created...");
class Laptop implements Item // class Laptop is of type Item
public Laptop(){

System.out.println("Latop Object Created...");


class Client
public static void main(String args[]){

Shelf shelfobj=new Shelf();

The above example is provided for beginners to practically copy and use the code for understanding.
Factory method pattern eliminate the need to bind application-specific classes into your code. The code only deals with the Product(Item) interface; therefore it can work with any user-defined ConcreteProduct(any objects could be stored in shelf) classes, as the Client class is not bound to Book,Laptop objects only we can create more type of objects by implementing Item interface and store in shelf.

There are multiple ways of implementing Factory Method which I will be providing samples in the forthcoming days.

To Practice
A customer visits to a shop and purchases the products of his preference for payment the customer might use debit card, credit card,customer card(VIP card with discount).Design a POS(Point of Sale) Reader class which will accept Credit card,Debit Card, Customer Card.

Readers may kindly provide valuable suggestions/comments in order to make the sample simple and easily understandable for beginners.

To have more discussions on Patterns and Object Oriented Design & Architecture, please feel free to mail me at :


  1. Hi Bala,
    Many More Congrates First...!

    Many More Thanks Second for this valuable endeavor...!

    I will update my comments after i reading completly...