Factory Design Pattern in Java with Example

In this article, we will look into how to implement a Factory design pattern in Java with an example.

Video

This article explained very well in below youtube video:

The factory design pattern overview

  • Factory Pattern is one of the Creational Design Pattern.
  • The Factory Design Pattern or Factory Method Pattern is one of the most used design patterns in Java.
  • In the Factory pattern, we create an object without exposing the creation logic to the client and refer to newly created objects using a common interface.
  • The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.

Factory Pattern Implementation

In this example, we'll create a Polygon interface which will be implemented by several concrete classes. A PolygonFactory will be used to fetch objects from this family:

Polygon Interface

Let's first create a Polygon interface:
package com.java.tutorials.factory;

public interface Polygon {
    public String getType();
}
Next, we'll create a few implementations like Square, Triangle, etc. that implement this interface and return an object of Polygon type.

Triangle Implementation

package com.java.tutorials.factory;

public class Triangle implements Polygon {

    @Override
    public String getType() {
        return "Triangle";
    }
}

Square Implementation

package com.java.tutorials.factory;

public class Square implements Polygon {

    @Override
    public String getType() {
        return "Square";
    }
}

Pentagon Implementation

package com.java.tutorials.factory;

public class Pentagon implements Polygon {

    @Override
    public String getType() {
        return "Pentagon";
    }
}

Octagon Implementation

package com.java.tutorials.factory;

public class Octagon implements Polygon {

    @Override
    public String getType() {
        return "Octagon";
    }
}

PolygonFactory Implementation

Now we can create a factory that takes the number of sides as an argument and returns the appropriate implementation of this interface:
package com.java.tutorials.factory;

public class PolygonFactory {

    public static Polygon getInstance(int sides) {

        Polygon polygon = null;

        switch (sides) {
            case 3:
                polygon = new Triangle();
                break;
            case 4:
                polygon = new Square();
                break;
            case 5:
                polygon = new Pentagon();
                break;
            case 8:
                polygon = new Octagon();
                break;
            default:
                // throw exception
                break;
        }

        return polygon;
    }
}

Client

Let's create a Client to test the factory design pattern. Client gets the instance of Polygon using PolygonFactory class:
package com.java.tutorials.factory;

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

        // triangle
        Polygon polygon = PolygonFactory.getInstance(3);
        System.out.println(polygon.getType());

        // square
        Polygon square = PolygonFactory.getInstance(4);
        System.out.println(square.getType());

        // pentagon
        Polygon pentagon = PolygonFactory.getInstance(5);
        System.out.println(pentagon.getType());

        // octagon
        Polygon octagon = PolygonFactory.getInstance(8);
        System.out.println(octagon.getType());
    }
}

Output

Triangle
Square
Pentagon
Octagon

When to Use Factory Method Design Pattern

  • When the implementation of an interface or an abstract class is expected to change frequently.
  • The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.
  • The lifetime management of the generated objects must be centralized to ensure consistent behavior within the application.
  • Advantage: The factory design pattern provides an approach to code for interface rather than implementation.
  • Advantage: The factory pattern removes the instantiation of actual implementation classes from the client code. The factory pattern makes our code more robust, less coupled and easy to extend.

Conclusion

In this article, we have seen what is a factory design pattern and how to factory design pattern in Java with an example.

Further learnings

Presentation Tier

Business Tier

Integration Tier


Comments