Factory Method Design Pattern.

Anik Dey
3 min readJul 7, 2019

So, the first question what is design pattern?.

In software engineering, we face some common recurring problems in software design. Design patterns help us solve these recurring design problems. There are 23 design patterns. When to use which design pattern depends on the situation.

If we want to create an object & the question is how to create it. Then we should go for creational design pattern which includes the following.

  1. Singleton
  2. Factory Method
  3. Abstract Factory
  4. Builder
  5. Prototype

In this article we are going to discuss about the Factory Method Pattern.

Factory method pattern is a creational pattern that uses factory methods for creating objects without specifying the exact class of object that is created. In factory method pattern the object creation logic is hidden and the newly created object is returned to the client using either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes (using a common interface).

  1. The factory class prevents coupling of the client code.

2. Instead of having multiple new MyObject() call, the initialization is kept in one place, satisfying the Don’t Repeat Yourself(DRY) principle.

3. New version of objects can easily be returned from that factory method. The derived classes must satisfy the Liskov Substitution Principle.

4. Factory design pattern provides approach to code for interface rather than implementation.

5. Factory pattern provides abstraction between implementation and client classes through inheritance.

Let us write some code to understand it.

Create an abstract class named Loan.

public abstract class Loan {

protected double interestRate;

abstract double getInterestRate();

public double calculateInterest(int loanAmount){
return loanAmount * (getInterestRate() / 100);
}

}

Now we are going to create two classes named HomeLoan & CarLoan. Both classes will extend the abstract Loan class and will provide their own implementation of getInterestRate().

public class HomeLoan extends Loan {

@Override
double getInterestRate() {
return 8;
}

}

For HomeLoan we are returning a random value 8.

public class CarLoan extends Loan {

@Override
double getInterestRate() {
return 9;
}
}

For CarLoan we are returning a random value 9.

Create an Enum like this.

public enum  LoanType {
HOME, CAR;
}

Now we are going to create our factory class that will provide the object for us.

public class LoanFactory {

public Loan getLoan(LoanType loanType) {

switch (loanType) {
case CAR:
return new CarLoan();
case HOME:
return new HomeLoan();

default:
return null;
}
}

}

We will use this Factory to get object of concrete classes by passing loan type.

Now create class named Main like this.

public class Main {

public static void main(String[] args) {

LoanFactory loanFactory = new LoanFactory();

Loan homeLoan = loanFactory.getLoan(LoanType.HOME);
System.out.println(homeLoan.calculateInterest(1000));

Loan carLoan = loanFactory.getLoan(LoanType.CAR);
System.out.println(carLoan.calculateInterest(1000));
}

}

Now what if we introduce a new type of loan named PersonalLoan. We don’t need to change anything in the client end. We just need to change in the factory class. Let us create a new class like this.

public class PersonalLoan extends Loan {

@Override
double getInterestRate() {
return 10;
}

}

As we have created a new type of loan change the enum like this.

public enum  LoanType {
HOME, CAR, PERSONAL;
}

Now change the Factory class like this.

public class LoanFactory {

public Loan getLoan(LoanType loanType) {

switch (loanType) {
case CAR:
return new CarLoan();
case HOME:
return new HomeLoan();
case PERSONAL:
return new PersonalLoan();

default:
return null;
}
}

}

And your Main class

public class Main {

public static void main(String[] args) {

LoanFactory loanFactory = new LoanFactory();

Loan homeLoan = loanFactory.getLoan(LoanType.HOME);
System.out.println(homeLoan.calculateInterest(1000));

Loan carLoan = loanFactory.getLoan(LoanType.CAR);
System.out.println(carLoan.calculateInterest(1000));

Loan personalLoan = loanFactory.getLoan(LoanType.PERSONAL);
System.out.println(personalLoan.calculateInterest(1000));
}

}

--

--