Factory Design Pattern – Implementation in Java

After discussing Singleton Pattern and Observer Pattern in my previous posts, I will be talking about Factory Design Pattern in this one.

What is a Factory Pattern?

Factory Pattern is also among the most commonly used design patterns. Instantiating an object with the new() operator couples the code tightly to the concrete implementation leading to less flexible code. The factory design pattern defines an interface for creating an object known as a factory method. It then uses this method to create the object.

The factory pattern allows you to create an object without specifying the exact class of object that needs to be created.

Advantages of Factory Design Pattern

  1. The factory design pattern removes the tight coupling in the application by delegating the instance creation to a factory method. The clients do not need to know the concrete implementations, it can code to the interface. Loose Coupling is the biggest advantage of Factory Design Patterns.
  2. It embodies the object oriented design principle of coding to an interface and not an implementation. Thereby making it easier to unit test the application.

Terminologies

  1. Creator Class : This is the class that has the factory method, and that returns an object of the concrete product class.
  2. Product Class : This is the interface or abstract class, from which the concrete product classes are supposed to inherit.
  3. Concrete Product Classes : One or more classes that should abide by the contract of the Product class. This is the class whose object ultimately gets created.

UML diagram

Java Implementation

Let us take an example application where we have to draw a shape.

For this the first step would be to create the object of whichever shape that needs to be drawn. And then the draw method on this shape needs to be called.

Typical method to do this would be something like

new Circle().draw();

If we have to now draw a rectangle, the above code would need to be modified to

new Rectangle().draw();

If we want to refactor the above code to use the Factory Pattern, we would have something like a base class Shape, which can be inherited by both Circle and Rectangle subclasses. We will have a ShapeFactory class that will give the object of the appropriate class based on input.

Let us look at the implementation below:

First, let us define the base product class Shape. Here we have made it as a Interface, but it can also be an abstract class.

public interface Shape {
String draw();
}
view raw Shape.java hosted with ❤ by GitHub

Next let us define define the concrete product classes.

public class Circle implements Shape {
@Override
public String draw() {
return "This is a circle !!!";
}
}
public class Rectangle implements Shape {
@Override
public String draw() {
return "This is a Rectangle !!!";
}
}
public class Square implements Shape {
@Override
public String draw() {
return "This is a Square !!!";
}
}
Next we have the class with the factory method.
public class ShapeFactory {
public Shape getShape(String shape) {
if (shape.equalsIgnoreCase("Circle"))
return new Circle();
else if (shape.equalsIgnoreCase("Rectangle"))
return new Rectangle();
else if (shape.equalsIgnoreCase("Square"))
return new Square();
return null;
}
}

You can find the complete code on github.

Factory Pattern in Java Library

The java.util.Calendar#getInstance() , java.util.ResourceBundle#getInstance() method uses the Factory design pattern. You can read more about the method in the Oracle docs.

2 thoughts on “Factory Design Pattern – Implementation in Java

Leave a Reply

Please log in using one of these methods to post your comment:

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