Java Language – 80 – Factory Method

Introduction to the Factory Method Pattern

The Factory Method pattern is a creational design pattern used in Java and other programming languages. It provides an interface for creating objects, but the specific type of objects to be created is determined by the subclass that implements the Factory Method. This pattern allows a class to delegate the responsibility of instantiating its objects to its subclasses.

Why Use the Factory Method Pattern?

The Factory Method pattern offers several advantages:

  • Flexibility: It allows for the creation of objects without specifying the exact class of object that will be created, providing flexibility in object creation.
  • Encapsulation: Object creation logic is encapsulated in the Factory Method, making it easy to change or extend object creation without modifying the client code.
  • Code Reusability: Subclasses can reuse the Factory Method, reducing duplication of object creation code.
Implementing the Factory Method Pattern

The Factory Method pattern can be implemented using an interface or an abstract class to declare the Factory Method, which is then overridden by concrete subclasses. Here’s an example of how to implement the Factory Method pattern in Java using an abstract class:


// Abstract creator class with the Factory Method
public abstract class Creator {
    public abstract Product factoryMethod();

    // Other methods can be added here
}

// Concrete creator subclass
public class ConcreteCreatorA extends Creator {
    @Override
    public Product factoryMethod() {
        return new ConcreteProductA();
    }
}

// Concrete product class
public class ConcreteProductA implements Product {
    // Implement product-specific methods and attributes
}

// Interface for product classes
public interface Product {
    // Define product-specific methods
}

In the example above, the abstract class Creator declares the Factory Method factoryMethod. Concrete creator subclasses like ConcreteCreatorA implement this method to create specific product instances like ConcreteProductA. The Product interface defines the common methods that product classes must implement.

Usage of the Factory Method Pattern

The Factory Method pattern is often used in situations where a class cannot anticipate the class of objects it must create. It allows for dynamic object creation, with the actual subclass being decided at runtime. This promotes loose coupling between the creator and the product, making the system more maintainable and extensible.

Comparing Factory Method and Simple Factory

The Factory Method pattern is sometimes confused with the Simple Factory pattern. While both patterns involve creating objects, they differ in their implementation and purpose. The Factory Method pattern delegates the responsibility of object creation to its subclasses, while the Simple Factory has a single method that handles the creation of objects based on an input parameter.

The Factory Method pattern is more flexible and allows for dynamic selection of the concrete factory at runtime. In contrast, the Simple Factory’s creation logic is contained within a single method, which is not extensible without modifying the factory class.

Conclusion

The Factory Method pattern is a valuable design pattern in Java, providing a way to create objects without specifying their exact class. It promotes flexibility, encapsulation, and code reusability in object creation. By allowing subclasses to determine the type of objects they create, the Factory Method pattern fosters loose coupling and enhances maintainability and extensibility in software design.