Facebook EmaiInACirclel
Design Patterns

Factory Method Design Pattern – Just a Little Builder

Eduard Ghergu
Eduard Ghergu
Software Architect

In object-oriented programming, the Factory Method is a Creational Design Pattern that allows us to create objects without specifying the exact class that will be instantiated. Instead of creating objects directly, we use a factory method to create and return them.

This Design Pattern promotes code reuse and loose coupling between classes making our code more modular and easier to maintain. It enables us to create objects without knowing the implementation details of our classes.

Factory Method

How does the Factory Method creational pattern work?

The Factory Method pattern consists of four main components:

  1. Product
  2. Concrete Product(s)
  3. Creator
  4. Concrete Creator(s)

The Product is the interface or abstract class that defines the methods that the ConcreteProduct must implement. The ConcreteProduct is the class that implements the Product interface. The Creator is the abstract class that declares the Factory Method. The ConcreteCreator is the class that implements the Factory Method and returns the ConcreteProduct.

creator-product dependency

 

An example of Factory Method project implementation

For a sample of how this Design Pattern can be put into practice, we are going to use our old friend, the furniture shop application introduced in the post related to Abstract Factory.

The furniture shop application is capable of producing furniture sets in different variants, like Modern, Classic, and Retro, with different products, like armchairs, sofas, and coffee tables being available for each variant. For each variant, we have a concrete factory class like RetroFurnitureFactory. This is responsible for creating a RetroArmchair, a RetroCoffeeTable, and RetroSofa products by using the MakeArmchair(), MakeCoffeeTable(), and MakeSofa() methods. In this case, the RetroFurnitureFactory is a Concrete Creator that creates the products using specific Factory Methods. The abstract Creator is, in our example, the FurnitureFactory abstract class, and the abstract Product is, at the very bottom of the inheritance hierarchy, the Product abstract class.

 

The Factory Method UML diagram for a C# code sample

Let’s see how the UML diagram looks for the above-described scenario:

Factory Method UML diagram

Let’s take a look at the main pros and cons of using this creational design pattern.

 

Advantages of the Factory Method Design Pattern

  1. Loose coupling – The Factory Method Design Pattern promotes loose coupling between classes. The Creator class doesn’t need to know the exact class that will be instantiated. This makes the code more modular and easier to maintain.
  2. Encapsulation – The Factory Method Design Pattern encapsulates the object creation process. This makes it easier to modify the creation process without affecting the rest of the code.
  3. Code reuse – The Factory Method Design Pattern promotes code reuse by allowing multiple classes to share a common interface.
  4. Extensibility – The Factory Method Design Pattern is extensible. It allows new ConcreteProduct classes to be added without modifying the existing code.

 

Disadvantages of the Factory Method Design Pattern

  1. Complexity – The Factory Method Design Pattern can add complexity to the code. It requires the creation of additional classes and interfaces.
  2. Overhead – The Factory Method Design Pattern can introduce additional overhead into the application. This can result in decreased performance if the creation of objects is a performance bottleneck.

 

Differences between Factory Method and Abstract Factory

The Factory Method and Abstract Factory are both creational design patterns used in object-oriented programming. While they share multiple similarities, such as encapsulating object creation by delegating it to subclasses and promoting loose decoupling between client code and the concrete classes, they also have specific use cases.

The main difference between the Factory Method and Abstract Factory design patterns is their level of abstraction and the way they create objects.

The Factory Method pattern defines an interface for creating objects, but it allows subclasses to decide which class to instantiate. It encapsulates the object creation logic in a separate method, which can be overridden by subclasses to create different types of objects. This pattern is useful when we want to create objects that belong to a single family of classes and defer instantiation to their subclasses.

On the other hand, the Abstract Factory pattern gives us a way to make groups of related or dependent objects without having to specify their concrete classes. It encapsulates a group of factories, each of which creates a family of objects that are related to each other.

 

When to use the Factory Method pattern?

The Factory Method pattern is useful when:

  1. We don’t know the exact class of object that will be created at runtime.
  2. We want to encapsulate the object creation process.
  3. We want to promote loose coupling between classes.
  4. We want to promote code reuse.

 

Conclusion

The Factory Method Design Pattern is a creational pattern that provides an interface for creating objects but allows subclasses to decide which class to instantiate. It is a pattern that promotes loose coupling between classes and promotes code reuse.

The Factory Method Design Pattern is particularly useful when we don’t know the exact class of object that will be created at runtime. In this blog post, we explored an example of Factory Method pattern implementation in C# and how it can be used to create objects.

We also discussed the advantages and disadvantages of the Factory Method Design Pattern and when to use it.

The C# 11 source code is available on GitHub.


Leave a Reply

Your email address will not be published. Required fields are marked *