Factory Method Pattern

Category : Design Patterns | Sub Category : Creational design patterns | By Prasad Bonam Last updated: 2023-07-09 08:21:18 Viewed : 339


Factory Method Pattern:

The Factory Method pattern is a creational design pattern that provides an interface for creating objects but allows subclasses to decide which class to instantiate. It encapsulates the object creation logic in a separate method, referred to as the "factory method," which is implemented by concrete subclasses. This pattern promotes loose coupling by decoupling the client code from the specific classes of objects being created. Here is an example of implementing the Factory Method pattern in Java:

java
// Product interface interface Product { void doSomething(); } // Concrete Product A class ConcreteProductA implements Product { @Override public void doSomething() { System.out.println("Doing something in Concrete Product A"); } } // Concrete Product B class ConcreteProductB implements Product { @Override public void doSomething() { System.out.println("Doing something in Concrete Product B"); } } // Creator (Abstract Factory) class abstract class Creator { public void someOperation() { // Perform some operations before creating the product Product product = createProduct(); product.doSomething(); // Perform some operations after creating the product } protected abstract Product createProduct(); } // Concrete Creator A class ConcreteCreatorA extends Creator { @Override protected Product createProduct() { return new ConcreteProductA(); } } // Concrete Creator B class ConcreteCreatorB extends Creator { @Override protected Product createProduct() { return new ConcreteProductB(); } } // Client code public class Client { public static void main(String[] args) { Creator creatorA = new ConcreteCreatorA(); creatorA.someOperation(); // Output: Doing something in Concrete Product A Creator creatorB = new ConcreteCreatorB(); creatorB.someOperation(); // Output: Doing something in Concrete Product B } }

In this example:

  • The Product interface defines the behavior that the concrete products must implement.
  • The ConcreteProductA and ConcreteProductB classes are the concrete implementations of the Product interface.
  • The Creator class acts as the abstract factory and declares the someOperation() method, which serves as the client is entry point to interact with the product. The createProduct() method is declared as abstract and is implemented by concrete subclasses.
  • The ConcreteCreatorA and ConcreteCreatorB classes are the concrete implementations of the Creator class. They override the createProduct() method to instantiate and return the specific Product implementation they are responsible for.
  • The client code demonstrates how the factory method is used. It creates instances of the concrete creators (ConcreteCreatorA and ConcreteCreatorB) and calls the someOperation() method. The someOperation() method internally calls the factory method createProduct() to create the appropriate product instance and performs operations on it.

By using the Factory Method pattern, you can delegate the responsibility of creating objects to subclasses, allowing for flexible object creation and decoupling the client code from specific product classes. This pattern promotes extensibility and enhances code maintainability.


Search
Related Articles

Leave a Comment: