Factory Method Design Pattern in Java (original) (raw)

Last Updated : 03 Jan, 2025

It is a creational design pattern that talks about the creation of an object. The factory design pattern says to define an interface ( A java interface or an abstract class) for creating the object and let the subclasses decide which class to instantiate.

factory-method

Table of Content

What is the Factory Method Design Pattern?

Factory Method Design Pattern define an interface for creating an object, but let subclass decide which class to instantiate. Factory Method lets a class defer instantiation to subclass.

What-is-Factory-Method-Design-Pattern

Below is the explanation of the above image:

When to use Factory Method Design Pattern?

Factory method design pattern can be used in java in following cases:

**Key Components of Factory Method Design Pattern

Below are the main components of Factory Method Design Pattern in Java:

Key-Component-of-Factory-Method-Design-Pattern-in-Java

Factory Method Design Pattern Example

Let's understand factory method design pattern using an example. Below is the problem statement to understand it:

You are developing a software system for an e-commerce platform that deals with various types of products. Each product category (e.g., electronics, clothing, books) requires specific handling during creation. However, you want to decouple the client code from the concrete product creation logic to enhance flexibility and maintainability. Additionally, you want to allow for easy extension by adding new product types in the future without modifying existing code.

1. Solution using Abstract Class

The above problem can be solved using Factory Method Design Pattern:

Java `

// Abstract Product Class abstract class Product { public abstract void display(); }

// Concrete Products class ConcreteProductA extends Product { @Override public void display() { System.out.println("This is Concrete Product A."); } }

class ConcreteProductB extends Product { @Override public void display() { System.out.println("This is Concrete Product B."); } }

// Creator Abstract Class abstract class Creator { public abstract Product factoryMethod(); }

// Concrete Creators class ConcreteCreatorA extends Creator { @Override public Product factoryMethod() { return new ConcreteProductA(); } }

class ConcreteCreatorB extends Creator { @Override public Product factoryMethod() { return new ConcreteProductB(); } }

// Client Code public class FactoryMethodExample { public static void main(String[] args) { Creator creatorA = new ConcreteCreatorA(); Product productA = creatorA.factoryMethod(); productA.display();

    Creator creatorB = new ConcreteCreatorB();
    Product productB = creatorB.factoryMethod();
    productB.display();
}

}

`

Output

This is Concrete Product A. This is Concrete Product B.

2. Solution using Interface

The above problem can be solved using Factory Method Design Pattern:

Java `

// Product Interface interface Product { void display(); }

// Concrete Products class ConcreteProductA implements Product { @Override public void display() { System.out.println("This is Concrete Product A."); } }

class ConcreteProductB implements Product { @Override public void display() { System.out.println("This is Concrete Product B."); } }

// Factory Interface interface Factory { Product factoryMethod(); }

// Concrete Factories class ConcreteFactoryA implements Factory { @Override public Product factoryMethod() { return new ConcreteProductA(); } }

class ConcreteFactoryB implements Factory { @Override public Product factoryMethod() { return new ConcreteProductB(); } }

// Client Code public class FactoryMethodExample { public static void main(String[] args) { Factory factoryA = new ConcreteFactoryA(); Product productA = factoryA.factoryMethod(); productA.display();

    Factory factoryB = new ConcreteFactoryB();
    Product productB = factoryB.factoryMethod();
    productB.display();
}

}

`

Output

This is Concrete Product A. This is Concrete Product B.

Use Cases of the Factory Method Design Pattern

Below are the use cases of factory method:

Advantages of Factory Method Design Pattern

Below are the main advantages of factory method:

Disadvantages of Factory Method Design Pattern

Below are the main advantages of factory method:

Conclusion

So far we learned what is Factory method design pattern and how to implement it. I believe now we have a fair understanding of the advantage of this design mechanism. Factory methods pervade toolkits and frameworks.The preceding document example is a typical use in MacApp and ET++.

Further Read__:_**Java Design Patterns Tutorial