![]() |
VOOZH | about |
The Factory Method Design Pattern is a Creational Design Pattern used to create objects without exposing the object creation logic to the client. It defines a method for creating objects, where subclasses decide which class object should be instantiated, making the application more flexible and maintainable.
👁 What-is-Factory-Method-Design-Pattern
Factory method design pattern can be used in java in following cases:
Note: The client may still use new product types, but it interacts through a common abstraction, so major client-side changes are usually not required.
Below are the main components of Factory Method Design Pattern in Java:
👁 Key-Component-of-Factory-Method-Design-Pattern-in-Java
Defines a common interface or abstract class for all objects created by the factory.
interface Product {
void display();
}
Implements the Product interface and defines specific behavior.
class ConcreteProductA implements Product {
public void display() {
System.out.println("Product A");
}
}
Declares the factory method that returns Product objects.
abstract class Creator {
abstract Product factoryMethod();
}
Implements the factory method and returns specific product objects.
class ConcreteCreatorA extends Creator {
Product factoryMethod() {
return new ConcreteProductA();
}
}
A method that creates and returns Product objects without exposing creation logic to the client.
Product p = new ConcreteCreatorA().factoryMethod();
We are developing an e-commerce system where different product categories (electronics, clothing, books) are created dynamically. You want to:
Defines a common structure for all product types.
abstract class Product {
public abstract void display();
}
These are actual product implementations created by factory.
Declares factory method but does not implement object creation.
abstract class Creator {
public abstract Product factoryMethod();
}
Each class decides which product object to create.
Client uses factory to create objects without knowing actual classes.
This is Concrete Product A. This is Concrete Product B.
Explanation: In this approach, a Creator interface defines the factory method, and different factory implementations create specific products. It provides more flexibility since Java allows multiple interface implementations
Defines contract for all product types.
interface Product {
void display();
}
Actual implementations of Product interface.
Defines factory contract for creating products.
interface Creator{
Product factoryMethod();
}
Each factory creates specific product type.
Client depends only on factory interface, not concrete classes.
This is Concrete Product A. This is Concrete Product B.
Explanation: In this approach, a Creator interface defines the factory method, and different factory implementations create specific products. It provides more flexibility since Java allows multiple interface implementations.
| Feature | Simple Object Creation | Factory Method Design Pattern |
|---|---|---|
| Object Creation | Directly using new keyword | Object creation is handled by factory method |
| Flexibility | Low flexibility | High flexibility |
| Coupling | Tightly coupled with concrete classes | Loosely coupled using interfaces |
| Code Maintenance | Hard to maintain if changes occur | Easy to maintain and extend |
| Scalability | Difficult to add new types | Easy to add new product types |
| Testing | Hard to mock objects | Easy to use mock objects |
| Complexity | Simple and straightforward | More complex due to extra classes |
| Best Use Case | Small/simple applications | Large and scalable applications |