Factory Pattern

A factory method pattern is a creational pattern. It is used to instantiate an object from one among a set of classes based on some logic. It is one of the core design pattern which is used heavily not only in JDK but also in various Open Source framework such as Spring, Struts etc. In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.

Let’s see an example of how factory pattern is implemented in Code. We have an Animal interface which is implemented by Dog and Cat class.

package mynotes.designPatterns.creational;

public interface Animal {

public void speak();

package mynotes.designPatterns.creational;

public class Dog implements Animal {

public void speak() {
System.out.println("Woof woof...");


package mynotes.designPatterns.creational;

public class Cat implements Animal {

public void speak() {


Lets have a abstract AnimalFactory class that have a createAnimal method which return object based on the type. The reson we are keeping this class an abstract class is because we do not want its object to be created. Usually factory classes cannot be instantiated.

package mynotes.designPatterns.creational;

public abstract class AnimalFactory {

public static final int CAT = 0;
public static final int DOG = 1;

public static Animal createAnimal(int type) {
switch (type) {
case CAT:
return new Cat();
case DOG:
return new Dog();
return null;


Following is our calling class:

package mynotes.designPatterns.creational;

public class App {

public static void main(String[] args) {
Animal animal = AnimalFactory.createAnimal(AnimalFactory.DOG);




Woof woof...

Advantage of Factory method Pattern in Java:

  • Factory method design pattern decouples the calling class from the target class, which result in less coupled and highly cohesive code. E.g.: JDBC is a good example for this pattern; application code doesn’t need to know what database it will be used with, so it doesn’t know what database-specific driver classes it should use. Instead, it uses factory methods to get Connections, Statements, and other objects to work with. Which gives you flexibility to change your back-end database without changing your DAO layer. Other example of factory in JDK are BorderFactory in Swing, java.sql.DriverManager#getConnection()  etc.
  • Factory pattern in Java enables the subclasses to provide extended version of an object, because creating an object inside factory is more flexible than creating an object directly in the client. Since client is working on interface level any time you can enhance the implementation and return from Factory.
  • Code written using Factory design pattern in Java is consistent and easy to debug and troubleshoot because you have a centralized method for object creation and every client is getting object from same place.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: