๐ญ Factory Method Pattern in Java — Step-by-Step Guide
The Factory Method Pattern is one of the key Creational Design Patterns in Java.
It provides an interface for creating objects, allowing subclasses or factory logic to decide which class to instantiate at runtime.
Rather than creating objects using the new keyword directly, the Factory hides the instantiation logic inside a dedicated class — improving flexibility, maintainability, and decoupling.
๐ฏ Real-World Analogy
Think of a person registration system.
Based on user input (M or F), we want to create a Male or Female object.
Instead of writing:
everywhere in the code, we delegate that logic to a Factory.
So the client simply calls:
and the factory takes care of the rest.
⚙️ Pattern Intent
-
Define an interface for object creation.
-
Let subclasses or logic decide which concrete class to instantiate.
-
Decouple object creation from object usage.
๐งฉ Step 1: Create an Interface (Person.java)
๐งฉ Step 2: Create Concrete Implementations
๐จ Male.java
๐ฉ Female.java
๐งฉ Step 3: Create the Factory Class
๐งฉ Step 4: Test the Factory
๐งพ Output
๐ง How It Works (Step-by-Step)
| Step | Action | Description |
|---|---|---|
| 1️⃣ | Client requests object creation | Calls factory.getPerson("M") |
| 2️⃣ | Factory checks input | Evaluates input value (“M” / “F”) |
| 3️⃣ | Factory creates instance | Uses new Male() or new Female() |
| 4️⃣ | Factory returns result | Returns Person reference to caller |
| 5️⃣ | Client uses interface | Calls say() without knowing the concrete type |
๐ Advantages of Factory Method Pattern
| Benefit | Description |
|---|---|
| ✅ Encapsulation | Object creation logic hidden from client |
| ✅ Loose Coupling | Client depends only on interface, not implementation |
| ✅ Extensibility | New types can be added without changing client code |
| ✅ Reusability | Common creation logic centralized in the factory |
| ✅ Simplicity | Cleaner and more readable code |
⚠️ When to Use Factory Method
-
When you don’t know the exact type of object to create at compile time.
-
When you want to isolate the creation logic.
-
When the object creation process is complex or conditional.
-
When you want to return subclasses or implementations dynamically.
๐งฉ UML-Like Text Diagram
๐งญ Real-World Examples in Java
| Use Case | Description |
|---|---|
Calendar.getInstance() | Returns different Calendar subclasses based on locale/timezone |
NumberFormat.getInstance() | Returns appropriate number format (e.g., for locale) |
JDBC DriverManager.getConnection() | Creates a connection for a given database type |
ExecutorService.newFixedThreadPool() | Factory method returning different thread pool types |
✅ Summary
| Concept | Description |
|---|---|
| Pattern Type | Creational |
| Purpose | Delegate object creation to a factory |
| Core Components | Interface, Concrete Classes, Factory Class |
| Key Benefit | Encapsulates creation logic, enhances flexibility |
| Real Example | PersonFactory creating Male or Female objects |
No comments:
Post a Comment