Factory Design Pattern vs Abstract Factory Design Pattern
Both patterns belong to the Creational Design Patterns category in the Gang of Four (GoF) design patterns. They deal with object creation but differ in scope and complexity.
Factory Design Pattern
Factory Pattern provides a way to create objects without exposing the creation logic to the client. The client uses a factory method to get an instance of a class.
Client --> Factory --> Product (Interface)
|--> ConcreteProductA
|--> ConcreteProductB
Example:
// Product interface
interface Shape {
void draw();
}
// Concrete products
class Circle implements Shape {
public void draw() { System.out.println("Drawing Circle"); }
}
class Square implements Shape {
public void draw() { System.out.println("Drawing Square"); }
}
// Factory
class ShapeFactory {
public Shape getShape(String type) {
if ("CIRCLE".equalsIgnoreCase(type)) return new Circle();
if ("SQUARE".equalsIgnoreCase(type)) return new Square();
return null;
}
}
// Usage
public class FactoryDemo {
public static void main(String[] args) {
ShapeFactory factory = new ShapeFactory();
Shape shape = factory.getShape("CIRCLE");
shape.draw();
}
}
Real-world Analogy
A pizza shop where you order by type: “Margherita” or “Pepperoni”. The shop decides how to make it.
Pros
- Centralized object creation.
- Hides complexity.
Cons
- Adding new types requires modifying factory logic (violates Open/Closed Principle).
Abstract Factory Design Pattern
Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Client --> AbstractFactory
|--> ConcreteFactoryA --> ProductA1, ProductA2
|--> ConcreteFactoryB --> ProductB1, ProductB2
Example
interface Button { void render(); }
interface Checkbox { void render(); }
class WindowsButton implements Button { public void render() { System.out.println("Windows Button"); } }
class WindowsCheckbox implements Checkbox { public void render() { System.out.println("Windows Checkbox"); } }
class MacButton implements Button { public void render() { System.out.println("Mac Button"); } }
class MacCheckbox implements Checkbox { public void render() { System.out.println("Mac Checkbox"); } }
interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}
class WindowsFactory implements GUIFactory {
public Button createButton() { return new WindowsButton(); }
public Checkbox createCheckbox() { return new WindowsCheckbox(); }
}
class MacFactory implements GUIFactory {
public Button createButton() { return new MacButton(); }
public Checkbox createCheckbox() { return new MacCheckbox(); }
}
public class Demo {
public static void main(String[] args) {
GUIFactory factory = new WindowsFactory();
factory.createButton().render();
factory.createCheckbox().render();
}
}
Real-world Analogy
A car parts factory that produces families of parts (engine + tires) for specific car brands (Toyota vs BMW).
Pros
- Supports multiple product families.
- Ensures consistency among related objects.
Cons
- More complex than Factory Pattern.
- Adding new families requires new factories.
Key Differences
| Aspect | Factory Pattern | Abstract Factory Pattern |
|---|---|---|
| Scope | Creates one type of object | Creates families of related objects |
| Complexity | Simple | More complex |
| Flexibility | Limited | High (supports multiple product families) |
When to Use Which
- Factory Pattern: When you need to create one type of object and want to hide creation logic.
- Abstract Factory Pattern: When you need to create multiple related objects that belong to different families.
Drawbacks
- Factory: Harder to extend without modifying factory logic.
- Abstract Factory: Higher complexity and more boilerplate.
Tag
Design Pattern Java



Post Comment