logo

Achieve Ultimate Excellence

Factory Method Design Pattern - Object Creation process with Flexibility in object instantiation

When it comes to designing software applications, one of the essential principles is to ensure that our code is flexible, maintainable, and easy to understand. Design patterns play a crucial role in achieving these goals by providing standardized solutions to common software design problems. One such design pattern is the "Factory Method" pattern. In this blog, we will delve into the Factory Method pattern, explore its concepts, and provide a detailed explanation along with Java code examples.

What is the Factory Method Pattern?

The Factory Method pattern is a creational design pattern that falls under the broader category of creational patterns. Creational patterns deal with the object creation process, providing flexibility in object instantiation while hiding the creation logic from the client code. The Factory Method pattern promotes the "Open/Closed" principle, which states that a class should be open for extension but closed for modification.

At its core, the Factory Method pattern defines an interface for creating objects but delegates the instantiation process to its subclasses. The pattern enables the client code to interact with objects through an abstract interface, while the specific concrete classes are determined by the subclasses.

How does it work?

To better understand the Factory Method pattern, let's break down its components:

  1. Product: This represents the interface of the objects the Factory Method creates. It defines the common methods that concrete products must implement.

  2. Concrete Product: These are the concrete implementations of the Product interface.

  3. Creator: The Creator is an abstract class or interface that declares the Factory Method responsible for creating objects of the Product type. It may also contain some default implementation for shared behavior.

  4. Concrete Creator: The Concrete Creator subclasses extend the Creator and provide specific implementations for the Factory Method to create particular Concrete Product instances.

The Factory Method pattern allows you to add new products (Concrete Product classes) without modifying the existing client code. This extensibility is what makes the pattern powerful and adaptable.

Java Code Explanation

1. Define the Product interface

// Product interface
  public interface Notification {
      void send();
  }

The Notification interface represents the contract that all concrete notification classes should adhere to. It declares the send() method, which will be implemented by the concrete notification classes.

2. Implement the Concrete Products

// Concrete Product: EmailNotification
  public class EmailNotification implements Notification {
      @Override
      public void send() {
          System.out.println("Sending an email notification...");
      }
  }

  // Concrete Product: SMSNotification
  public class SMSNotification implements Notification {
      @Override
      public void send() {
          System.out.println("Sending an SMS notification...");
      }
  }

The EmailNotification and SMSNotification classes are concrete implementations of the Notification interface. They provide specific implementations of the send() method for sending email and SMS notifications, respectively.

3. Create the Creator (abstract class) with the Factory Method

// Creator (abstract class)
  public abstract class NotificationCreator {
      public abstract Notification createNotification();
  }

The NotificationCreator class is an abstract class that serves as the Creator in the Factory Method pattern. It declares the abstract method createNotification(), which returns a Notification object. This method is responsible for creating different types of notifications, but the actual implementation is deferred to the concrete subclasses.

4. Implement the Concrete Creators

// Concrete Creator: EmailNotificationCreator
  public class EmailNotificationCreator extends NotificationCreator {
      @Override
      public Notification createNotification() {
          return new EmailNotification();
      }
  }

  // Concrete Creator: SMSNotificationCreator
  public class SMSNotificationCreator extends NotificationCreator {
      @Override
      public Notification createNotification() {
          return new SMSNotification();
      }
  }

The EmailNotificationCreator and SMSNotificationCreator classes are Concrete Creators. They extend the NotificationCreator class and provide specific implementations of the createNotification() method for creating instances of EmailNotification and SMSNotification objects, respectively.

5. Client code that uses the Factory Method to create notifications

public class Client {
      public static void main(String[] args) {
          // Creating an Email notification
          NotificationCreator emailCreator = new EmailNotificationCreator();
          Notification emailNotification = emailCreator.createNotification();
          emailNotification.send();

          // Creating an SMS notification
          NotificationCreator smsCreator = new SMSNotificationCreator();
          Notification smsNotification = smsCreator.createNotification();
          smsNotification.send();
      }
  }

In the client code, we use the Factory Method pattern to create different types of notifications without being concerned about the creation logic. The client code interacts with the notifications through the Notification interface, promoting loose coupling and making the code more maintainable and extensible.

Conclusion

The Factory Method pattern is a valuable tool in a developer's arsenal when it comes to designing flexible and extensible software systems. By encapsulating the object creation process and providing an abstract interface for clients, it promotes loose coupling and makes the code easier to manage. Understanding and applying design patterns like the Factory Method pattern is essential for writing clean, maintainable, and efficient code. So, the next time you find yourself in a situation where you need to create objects in a flexible way, consider using the Factory Method pattern to achieve your design goals.

avatar
Article By,
Create by
Browse Articles by Related Categories
Browse Articles by Related Tags
Share Article on:

Related posts

Related posts