Factory Pattern

Factory Design Pattern is the most widely used design pattern in programming languages.It comes under Creational Design Patterns.To understand what is Creational Design Patten please read the introduction in the post Creational Design Pattern.

Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class.

A factory is a Java class that is used to encapsulate object creation code. A factory class instantiates and returns a particular type of object based on data passed to the factory. The different types of objects that are returned from a factory typically are subclasses of a common parent class.

The data passed from the calling code to the factory can be passed either when the factory is created or when the method on the factory is called to create an object. This creational method is often called something such as getInstance or getClass .

Lets see a very simple example, let’s create an AnimalFactory class that will return an animal object based on some data input. so lets construct an abstract Animal class. The factory will return an instantiated subclass of Animal. Animal has a single abstract method, makeSound().

Animal.java

The Dog class is a subclass of Animal. It implements makeSound() to return “Woof”.

Dog.java

The Cat class is a subclass of Animal. It implements makeSound() to return “Meow”.

Cat.java

Let’s implement our factory. Now we will call our factory’s object creation method getAnimal(String type) and pass String as a parameter. If the String is “canine“, it returns a Dog object. Otherwise, it returns a Cat object.

AnimalFactory.java

The Demo class demonstrates the use of our factory. It creates an AnimalFactory factory. The factory creates an Animal object and then another Animal object. The first object is a Cat and the second object is a Dog. The output of each object’s makeSound() method is displayed.

Demo.java

The console output is shown here.

Console Output

a1 sound: Meow
a2 sound: Woof

 

Explanation:
Here the factory has encapsulated Animal object creation code, thus resulting in clean code in Demo, the class that creates the factory. Additionally, notice the use of polymorphism. We obtain different Animal objects (Cat and Dog) based on data passed to the factory.

Note that it is common to pass data that determines the type of object to be created to the factory when the factory is created (via the factory constructor). However, if multiple objects are being created by the factory, it may make sense to pass this data to the factory’s creational method rather than to the constructor, since it might not make sense to create a new factory object each time we wanted to have the factory instantiate a new object.

A factory may also be used in conjunction with the singleton pattern. It is common to have a singleton return a factory instance. To do this, we could replace:

AnimalFactory animalFactory = new AnimalFactory();

with

AnimalFactory animalFactory = AnimalFactory.getAnimalFactoryInstance();

In this example, AnimalFactory.getAnimalFactoryInstance() would be implemented to return a static AnimalFactory object. This results in a single factory being instantiated and used rather than requiring a new factory to be instantiated each time the factory needs to be used.

Thats all about Factory design pattern friends.. 🙂

   

Leave a Reply

Your email address will not be published.