What is Factory pattern?

The Factory pattern is a creational design pattern that provides an interface or base class for creating objects but delegates the responsibility of instantiation to subclasses or concrete factory implementations. It allows the creation of objects without specifying their exact class, promoting loose coupling and enhancing flexibility in object creation.

Key components of the Factory pattern:

  1. Product: This refers to the object that the factory creates. It is typically an interface, abstract class, or a common base class shared by multiple concrete implementations.
  2. Factory: The factory class provides a method or set of methods for creating objects of the product type. It encapsulates the creation logic and is responsible for instantiating and returning the appropriate concrete instances.
  3. Concrete Product: These are the specific implementations of the product interface or base class. Each concrete product represents a specific variation or configuration of the product.

Benefits and use cases of the Factory pattern:

  1. Encapsulation and Abstraction: The Factory pattern encapsulates the object creation process, hiding the details of object instantiation and providing a unified interface for clients to obtain instances of the product. This promotes encapsulation and abstraction, allowing clients to work with the product without worrying about its creation logic.
  2. Flexible Object Creation: By delegating the responsibility of object creation to subclasses or concrete factories, the Factory pattern allows for flexible and dynamic creation of objects. New products can be easily added by creating corresponding concrete product classes and factories without modifying the existing codebase.
  3. Loose Coupling: Clients of the factory only depend on the product interface or base class, rather than concrete implementations. This reduces coupling and promotes code flexibility, as the clients are decoupled from the specific product implementations.
  4. Code Organization and Maintainability: The Factory pattern helps in organizing object creation logic in a centralized manner, improving code readability and maintainability. It separates the responsibility of object creation from the client code, making it easier to modify or extend the creation process without affecting the clients.

Common variations of the Factory pattern include:

  1. Simple Factory: A simple factory uses a single factory method to create different types of objects based on a parameter or input. The creation logic resides within the factory method.
  2. Factory Method: The factory method pattern defines an abstract method in the factory base class or interface, which the concrete subclasses override to provide the specific product instantiation logic. Each concrete factory is responsible for creating a particular product type.
  3. Abstract Factory: The abstract factory pattern provides an interface or base class for creating families of related or dependent objects. It encapsulates a group of factory methods, each responsible for creating a different product type within the same family. The abstract factory allows the creation of products that are designed to work together.

Here’s a basic example of the Factory Method pattern in Java:

public interface Product {
    void operation();
}

public class ConcreteProduct implements Product {
    @Override
    public void operation() {
        System.out.println("Performing operation in ConcreteProduct");
    }
}

public abstract class Factory {
    public abstract Product createProduct();

    // Other factory methods or logic
}

public class ConcreteFactory extends Factory {
    @Override
    public Product createProduct() {
        return new ConcreteProduct();
    }
}

// Client code
public class Main {
    public static void main(String[] args) {
        Factory factory = new ConcreteFactory();
        Product product = factory.createProduct();
        product.operation();
    }
}

In the above example, the Product interface represents the abstract product, the ConcreteProduct class is a specific implementation of the product, and the Factory class defines the abstract factory with the createProduct() method. The ConcreteFactory subclass overrides the createProduct() method to instantiate and return a ConcreteProduct instance.

Using the Factory Method pattern, the client code can create a product by invoking the createProduct() method on the factory. The factory decides which concrete product to create based on its implementation.

The Factory pattern provides a way to decouple the client code from the specific product classes and allows for flexibility and extensibility in object creation.

error: Content is protected !!