Implement Factory Design Pattern in Java with an Example

The Factory Design Pattern is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of created objects. This pattern promotes loose coupling and helps manage object creation, making it easier to introduce new types without modifying existing code.

Example: Factory Design Pattern

In this example, we will create a simple notification system that can send different types of notifications (e.g., Email and SMS). We’ll use a factory to create these notification objects based on the type specified.

Step 1: Define the Notification Interface

// Notification.java
public interface Notification {
    void notifyUser();
}

Step 2: Create Concrete Implementations

// EmailNotification.java
public class EmailNotification implements Notification {
    @Override
    public void notifyUser() {
        System.out.println("Sending Email Notification");
    }
}

// SMSNotification.java
public class SMSNotification implements Notification {
    @Override
    public void notifyUser() {
        System.out.println("Sending SMS Notification");
    }
}

Step 3: Create the Notification Factory

// NotificationFactory.java
public class NotificationFactory {
    public static Notification createNotification(String type) {
        if (type == null) {
            return null;
        }
        if (type.equalsIgnoreCase("EMAIL")) {
            return new EmailNotification();
        } else if (type.equalsIgnoreCase("SMS")) {
            return new SMSNotification();
        }
        return null;
    }
}

Step 4: Using the Factory in the Main Class

// Main.java
public class Main {
    public static void main(String[] args) {
        // Create Email Notification
        Notification emailNotification = NotificationFactory.createNotification("EMAIL");
        emailNotification.notifyUser();

        // Create SMS Notification
        Notification smsNotification = NotificationFactory.createNotification("SMS");
        smsNotification.notifyUser();
    }
}

Explanation of the Code

  1. Notification Interface: This defines the method notifyUser() that will be implemented by concrete notification classes.
  2. Concrete Implementations:
  • EmailNotification: Implements the Notification interface to send email notifications.
  • SMSNotification: Implements the Notification interface to send SMS notifications.
  1. NotificationFactory: This factory class contains a static method createNotification() that takes a type string as input and returns an instance of the appropriate notification class based on the type provided.
  2. Main Class: In the main method, we use the NotificationFactory to create instances of EmailNotification and SMSNotification without needing to know the specifics of their implementations. We simply call notifyUser() on the created objects.

Output

When you run the Main class, the output will be:

Sending Email Notification
Sending SMS Notification

Conclusion

The Factory Design Pattern allows you to create objects without specifying the exact class of object that will be created. This provides flexibility and scalability in your codebase, making it easier to add new types of objects in the future without modifying existing code.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *