Factory Design Pattern in Java

Neha Gupta
4 min readJun 2, 2023

--

Hello reader 👋

While studying low level design we must have came across different design patterns and trust me these blow off our heads 😵. So one such design pattern is Factory Design Pattern which is a creational design pattern . So , today in this blog we are going to have a deep dive into Factory Design Pattern . We will cover its basic definition , implementation , advantages and some real world examples .

Basic Definition of Factory Design Pattern

The Factory Design Pattern is a creational design pattern that provides an interface for creating objects without specifying their concrete classes. It says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate.

To understand this definition more clearly let’s take an example suppose you went to an Apple store and you asked them for IPhone14 they will check the availability of it and will simply hand it over to you . Now what’s happening here is you are client and Apple store is factory , you don’t have to worry about how this Apple store is going to get IPhone14 and how it is going to serve you with IPhone14 , this complete process is hidden from you and this is what Factory Design Pattern does .

Components of Factory Design Pattern

The basic components of Factory Design Pattern ->

  1. Factory Class -: The factory class is responsible for creating and returning instances of the concrete products.
  2. Product Interface/Abstract Class -: This defines the interface or abstract class that represents the products to be created by the factory.
  3. Concrete Products -: These are the specific classes that implement the product interface or extend the abstract class.

So in the above example Apple Store is our factory , Phone is our product interface and IPhone14 , IPhone14 Pro and IPhone14 Pro Max are our product classes . Note that we have an Unavailable Phone class as product class this is going to give us the relevant information about unavailability of any phone in store .

Implementation of Factory Design Pattern

Let’s have a quick look on the implementation of Factory Design Pattern . For implementation we are going to use our Apple Store example .

Step 1

Create product interface .

Phone Class

public abstract class Phone {
abstract double getPrice();
abstract String getdescription();
}

Step 2

Create concrete products .

IPhone14 class

public class IPhone14 extends Phone {
public IPhone14(){

}
@Override
public String getdescription(){
return "This is IPhone14";
}
@Override
public double getPrice(){
return 55000.00;
}
}

IPhone14 Pro class

public class IPhone14Pro extends Phone{
public IPhone14Pro(){

}
@Override
public String getdescription(){
return "This is IPhone14 Pro";
}
@Override
public double getPrice(){
return 80000.00;
}
}

IPhone14 Pro Max class

public class IPhone14ProMax extends Phone{
public IPhone14ProMax(){

}
@Override
public String getdescription(){
return "This is IPhone14 Pro Max";
}
@Override
public double getPrice(){
return 125000.00;
}
}

Unavailable Phone class

public class UnavailablePhone extends Phone{
public UnavailablePhone(){

}
@Override
public String getdescription(){
return "This phone is not available";
}
@Override
public double getPrice(){
return 0.00;
}
}

Step 3

Create a Factory Class .

AppleStore class

import java.util.Objects;

public class AppleStore {
public static Phone getPhone(String name){
if(Objects.equals(name,"IPhone14"))
return new IPhone14();
else if (Objects.equals(name,"IPhone14Pro"))
return new IPhone14Pro();
else if (Objects.equals(name,"IPhone14ProMax"))
return new IPhone14ProMax();
else
return new UnavailablePhone();
}
}

Step 4

Create a client class .

Client class

public class Client {
public static void main(String []args){
Phone iphone14=AppleStore.getPhone("IPhone14");
System.out.println(iphone14.getdescription());
System.out.println(iphone14.getPrice());
System.out.println();
Phone iphone13=AppleStore.getPhone("IPhone13");
System.out.println(iphone13.getdescription());
System.out.println(iphone13.getPrice());
}
}

Output

So let’s see how this complete code is actually working . You are a client so you made a request for IPhone 14 to Apple Store , now this Apple Store is going to check each and every concrete product , if found it will give you an object otherwise it will give an Unavailable Phone object .

Advantages of Factory Design Pattern

  • Factory Method Pattern allows the sub-classes to choose the type of objects to create.
  • It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.

Real World Examples

  1. Database Connection Factories: Database connection factories are a common example of the Factory pattern. The factory class can examine the parameters (such as database type, credentials, etc.) and create the appropriate database connection object, hiding the specific implementation details from the client.
  2. GUI Widget Libraries: Graphical user interface (GUI) widget libraries often use the Factory pattern to create different types of widgets. The factory class can determine the requested widget type and return the corresponding concrete widget implementation.
  3. Logging Systems: Logging frameworks utilize the Factory pattern to create loggers with different configurations. The factory class can take in parameters like log level, log file destination, or log format, and instantiate the appropriate logger based on those parameters.

So this is it , this was my knowledge on Factory Design Pattern . Please leave some claps and don’t forget to follow me 💙.

Complete code at 👉https://github.com/Neha611/Factory-Design-Pattern

Thank you 😄

--

--