Java: Factory design pattern with example

Factory method is a creational design pattern, i.e., related to object creation. In Factory pattern, we create object without exposing the creation logic to client and the client use the same common interface to create new type of object.
The idea is to use a static member-function (static factory method) which creates & returns instances, hiding the details of class modules from user.

A factory pattern is one of the core design principles to create an object, allowing clients to create objects of a library(explained below) in a way such that it doesn’t have tight coupling with the class hierarchy of the library.

Factory Design Pattern Advantages
  • Factory design pattern provides approach to code for interface rather than implementation.
  • Factory pattern removes the instantiation of actual implementation classes from client code. Factory pattern makes our code more robust, less coupled and easy to extend. For example, we can easily change PC class implementation because client program is unaware of this.
  • Factory pattern provides abstraction between implementation and client classes through inheritance.

We're going to create a Calculator interface and concrete classes implementing the Calculator interface. A factory class CalculatorFactory is defined as a next step.
FactoryDemoClass our demo class will use CalculatorFactory to get a Calculator object. It will pass information ADD/ DIFF/ MULT/DIV) to CalculatorFactory to get the type of object it needs

1.Create an Interface:

public interface Calculator {

public void calculation(float num1, float num2);

2.Create concrete classes implementing the same interface:

public class Addition implements Calculator {

public void calculation(float num1, float num2) {

System.out.println("Sum: " + (num1 + num2));

public class Difference implements Calculator {

public void calculation(float num1, float num2) {

System.out.println("Difference : " + (num1 - num2));

public class Division implements Calculator {

public void calculation(float num1, float num2) {

System.out.println("Division result: " + (num1 / num2));

public class Multiplication implements Calculator {

public void calculation(float num1, float num2) {

System.out.println("Multiplication result: " + (num1 * num2));

Create a Factory to generate object of concrete class based on given information.

public class CalculatorFactory {

public static Calculator getInstance(String method) {

if (method.equals("ADD")) {
return new Addition();
} else if (method.equals("DIFF")) {
return new Difference();
} else if (method.equals("MULT")) {
return new Multiplication();
} else if (method.equals("DIV")) {
return new Division();

throw new NullPointerException("Method not found");

Use the Factory to get the object of the concrete class by passing information such as type.

public class FactoryDemoClass {

public static void main(String[] args) {

Calculator add = CalculatorFactory.getInstance("ADD");
add.calculation(4, 7);
Calculator diff = CalculatorFactory.getInstance("DIFF");
diff.calculation(4, 7);
Calculator div = CalculatorFactory.getInstance("DIV");
div.calculation(4, 7);
Calculator mult = CalculatorFactory.getInstance("MULT");
mult.calculation(4, 7);


Sum: 11.0
Difference : -3.0
Division result: 0.5714286
Multiplication result: 28.0

by sibin


  1. Genuine and Free jobs!
    Don't miss any job opportunities!
    Improve your life, only one step!

    More Java jobs info, Please join the Java Jobs group:


Post a Comment