Factory Method Pattern
November 5th, 2009A factory method pattern is a creational pattern. It is used to instantiate an object from one among a set of classes based on a logic.
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.
Sample factory method design pattern implementation in Java API
For a reference of how the factory method design pattern is implemented in Java, you can have a look at SAXParserFactory. It is a factory class which can be used to intantiate SAX based parsers to pares XML. The method newInstance is the factory method which instantiates the sax parsers based on some predefined logic.
Block diagram for The Design Pattern

Sample Java Source Code for Factory Method Design Pattern
Based on comments received from users, I try to keep my sample java source code as simple as possible for a novice to understand.
Base class:
package com.javapapers.sample.designpattern.factorymethod;
//super class that serves as type to be instantiated for factory method pattern
public interface Pet {
public String speak();
}
First subclass:
package com.javapapers.sample.designpattern.factorymethod;
//sub class 1 that might get instantiated by a factory method pattern
public class Dog implements Pet {
public String speak() {
return "Bark bark...";
}
}
Second subclass:
package com.javapapers.sample.designpattern.factorymethod;
//sub class 2 that might get instantiated by a factory method pattern
public class Duck implements Pet {
public String speak() {
return "Quack quack...";
}
}
Factory class:
package com.javapapers.sample.designpattern.factorymethod;
//Factory method pattern implementation that instantiates objects based on logic
public class PetFactory {
public Pet getPet(String petType) {
Pet pet = null;
// based on logic factory instantiates an object
if ("bark".equals(petType))
pet = new Dog();
else if ("quack".equals(petType))
pet = new Duck();
return pet;
}
}
Using the factory method to instantiate
package com.javapapers.sample.designpattern.factorymethod;
//using the factory method pattern
public class SampleFactoryMethod {
public static void main(String args[]){
//creating the factory
PetFactory petFactory = new PetFactory();
//factory instantiates an object
Pet pet = petFactory.getPet("bark");
//you don't know which object factory created
System.out.println(pet.speak());
}
}
Output of the above sample program for Factory Method Pattern
Bark bark



Nice!!. Though I am using all but when i try to related to design pattern, normally it looks me like big sky infront of me.
But you explined neatly.
Thanks for your effort and time for writing this ariticle.
Thanks
Thamayanthi
Factory is one of the widely used design patterns. Happy that you are able to relate it. Good luck Thamayanthi!
Tutorials and articles available on internet try to be comprehensive and consequently confuses the reader. I try to keep my definitions as simple as possible for users to get the fundamental understanding right. So that, over experience they can build on top of it.
Design patterns are not a complex subject. With appropriate tutorials it will be interesting to learn and use.
actualy i was using this design pattern only for creating object but not for return the object of super class based on logic.
so it is now clear to use this design pattern
thanks
Best wishes Rampratap.
[...] To keep things simple you can understand it like, you have a set of ‘related’ factory method design pattern. Then you will put all those set of simple factories inside a factory pattern. So in turn you need [...]
Abstract Factory Pattern&hellip on November 11th, 2009 9:31 pmThis is good article. explained in very clear understanding way.
Thanks for your post.
Thanks
Simple and Nice explanation. Able to understand the basic defenition of the Factory methods.
Nice and simplified explaination of a good design pattern..Good effort
Really good. I have understood it now. Thanks a lot
The example is good. but to make it more apparent with the definition put the public Pet getPet(String petType) in an interface and let PetFactory inplement this interface. Reason any conventional definition tell you that there are four aspects to Factory Method.
1. Product — in your case it is Pet. The interface of objects created by the factory
2. Concrete Product — in your case it is Dog and Duck .The implementing class of Product. Object of this class is created by ConcreteCreator.
3. Creator — Which is the interface i am talking about. This interface defines the factory methods
4. Concrete Creator — PetFactory in your case. This class that extends Creator and that provides an implementation for the factoryMethod. This can return any object that implements Product interface.
This will give the readers exact implementation for Factory Method.