Factory Method
What is Factory Method?
The Factory Method is a creational design pattern used in Java to hide the object creation logic from the client. Instead of using new, we let the subclasses decide which object to create.
The Factory Method is a creational design pattern used in Java to hide the object creation logic from the client. Instead of using new, we let the subclasses decide which object to create.
Why use it?
Concept
Implementation - Step by Step with Real Example
1. Notification.java (Interface)
2. Subclasses (Implement the interface)
SmsNotification.java
EmailNotification.java
PushNotification.java
3. NotificationFactory.java (Factory class)
4. NotificationService.java (Client/Driver Code)
Output:
- To hide complex object creation logic
- To support loose coupling
- To provide polymorphic object creation
- To follow SOLID principles, especially Open/Closed Principle
Concept
Term | Meaning |
---|---|
Interface | Common behavior that all notification types share ( notifyUser() ) |
Subclasses | Each subclass (like SMS, Email) defines how to send a notification |
Factory Class | creates the correct object based on input (channel) |
Client (Main class) | Asks factory for object, and uses it - without knowing creation details |
Implementation - Step by Step with Real Example
1. Notification.java (Interface)
package com.factorymethod;
public interface Notification { void notifyUser();}
2. Subclasses (Implement the interface)
SmsNotification.java
package com.factorymethod;
public class SmsNotification implements Notification { public void notifyUser() { System.out.println("Sending an SMS notification!!"); }}
EmailNotification.java
package com.factorymethod;
public class EmailNotification implements Notification { public void notifyUser() { System.out.println("Sending an Email notification!!"); }}
PushNotification.java
package com.factorymethod;
public class PushNotification implements Notification { public void notifyUser() { System.out.println("Sending a Push notification!!"); }}
3. NotificationFactory.java (Factory class)
package com.factorymethod;
public class NotificationFactory { public Notification createNotification(String channel) { if (channel == null || channel.isEmpty()) { return null; }
switch (channel.toUpperCase()) { case "SMS": return new SmsNotification(); case "EMAIL": return new EmailNotification(); case "PUSH": return new PushNotification(); default: throw new IllegalArgumentException("Unknown channel: " + channel); } }}
4. NotificationService.java (Client/Driver Code)
package com.factorymethod;
public class NotificationService { public static void main(String[] args) { NotificationFactory factory = new NotificationFactory();
// You can change the string to "EMAIL" or "PUSH" to get different outputs Notification notification = factory.createNotification("SMS");
notification.notifyUser(); }}
Output:
Sending an SMS notification!!
Analogy
Imagine Zomato wants to send order updates to users through SMS, Email, or App Notification. Instead of writing 3 new objects, you can use a factory to generate whichever is needed - neat, clean, and scalable.