The Factory Method Design Pattern: A Modular Approach to Object Creation

Apr 15th - 2024
By
Belatrix

Exploring the Factory Method Design Pattern in Object-Oriented Programming

In object-oriented programming, the Factory Method is a Creational Design Pattern that provides a way to instantiate objects without specifying the precise class of object that will be created. This pattern involves using a factory method to create and return object instances, rather than instantiating objects directly.

This design pattern enhances code modularity and reduces tight coupling between classes, making the codebase more maintainable. It allows for the creation of objects without exposing the instantiation logic to the client and without the client knowing about which class will be instantiated.

Factory Method Design Pattern

How the Factory Method Pattern Operates

The Factory Method design pattern is structured around four key components:

  • Product: This is an interface or abstract class that outlines the methods to be implemented by the ConcreteProduct.
  • Concrete Product(s): These are the classes that implement the Product interface.
  • Creator: An abstract class that declares the factory method.
  • Concrete Creator(s): Classes that implement the factory method and return an instance of the ConcreteProduct.

Implementing the Factory Method in a Project

To illustrate the Factory Method design pattern in action, consider a furniture shop application that produces furniture sets in various styles, such as Modern, Classic, and Retro. Each style has different products like armchairs, sofas, and coffee tables. For each style, there is a concrete factory class, such as RetroFurnitureFactory, responsible for creating RetroArmchair, RetroCoffeeTable, and RetroSofa products using specific factory methods like MakeArmchair(), MakeCoffeeTable(), and MakeSofa(). In this scenario, RetroFurnitureFactory acts as a Concrete Creator, while the abstract Creator is represented by the FurnitureFactory class, and the Product is represented by an abstract class at the base of the inheritance hierarchy.

The Factory Method Design Pattern UML Diagram

The UML diagram for the Factory Method pattern in a C# code sample would illustrate the relationships between the Product, Concrete Product(s), Creator, and Concrete Creator(s).

Pros and Cons of the Factory Method Design Pattern

Advantages:

  • Loose Coupling: The pattern encourages loose coupling by not requiring the Creator class to know the exact class of the object that will be instantiated.
  • Encapsulation: It encapsulates the object creation process, allowing changes to be made to the creation process without impacting the rest of the code.
  • Code Reuse: By allowing multiple classes to share a common interface, the pattern promotes code reuse.
  • Extensibility: New ConcreteProduct classes can be added without altering existing code, making the pattern extensible.

Disadvantages:

  • Complexity: The pattern can introduce complexity by necessitating additional classes and interfaces.
  • Overhead: It may add overhead to the application, potentially affecting performance if object creation is a bottleneck.

Factory Method vs. Abstract Factory

While both the Factory Method and Abstract Factory are creational design patterns, they differ in their abstraction level and object creation methods. The Factory Method pattern allows subclasses to determine which class to instantiate and is ideal for creating a single family of classes. The Abstract Factory pattern, however, is used to create families of related or dependent objects without specifying their concrete classes.

When to Use the Factory Method Pattern

The Factory Method Design Pattern is beneficial when:

  • The exact class of the object to be created is not known at runtime.
  • The object creation process needs to be encapsulated.
  • Loose coupling between classes is desired.
  • Code reuse is a priority.

Conclusion

The Factory Method Design Pattern is a valuable tool for creating objects in a way that promotes loose coupling and code reuse. It is especially useful when the class of the object to be created is not known until runtime. This article has explored a C# example of the Factory Method pattern and discussed its benefits and potential drawbacks, as well as appropriate use cases.

The C# 11 source code for this pattern is available on GitHub for further exploration.

Apr 15th - 2024
More Stories for you
Discover why MySQL is a trusted database for developers, offering scalability, reliability, and cost-effectiveness. Learn about the key benefits of MySQL and how MySQL HeatWave is transforming real-time analytics, mixed workloads, transactional processing, and machine learning for modern software development.

The importance of MySQL for developers

Mar 12th - 2025
MySQL is one of the most widely used and trusted databases in the world, powering applications across various industries. MySQL provides a scalable, reliable, and cost-effective solution for data management...
Learn how Power BI consulting services can help businesses optimize their analytics potential, particularly through the integration of Copilot’s AI capabilities, which unlocks new opportunities for data-driven decision-making and insights.

Power BI consulting services opportunities: Linking Copilot with Power BI 

Mar 5th - 2025
It’s a data-driven world today, and businesses are constantly seeking ways to optimize their analytics potential. Power BI consulting services have emerged as a critical solution for organizations that want to maximize...

Subscribe

Stay up-to-date with our latest insights