Mastering the Art of the Factory Method Design Pattern

Mastering the Art of the Factory Method Design Pattern

Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be?created.

in other words, it defines an interface for creating an object, but leaves the choice of its type to the subclasses, creation being deferred at run-time.

so the main Usage is to create objects without specifying the exact class of object that will be created.

it addresses the problem when adding a new class to the program isn’t that simple if the rest of the code is already coupled to existing?classes.

The Factory Method pattern recommends using a special factory method instead of directly creating objects with the new operator. This method still uses the new operator internally to create objects and the objects it returns are called products.

so it did solve the problem by letting You customize the factory method in a subclass to create different types of products by changing the class of the products being created.

the main Advantages are:

- Encapsulates object creation.

- Promotes loose coupling.

- Enhances code maintainability.


Class Diagram:

factory method class diagram

The participant's classes in this pattern are:

  • Product defines the interface for objects the factory method creates.
  • ConcreteProduct implements the Product interface.
  • Creator(also referred to as Factory because it creates the Product objects) declares the method FactoryMethod, which returns a Product object. May call the generating method for creating Product objects
  • ConcreteCreator overrides the generating method for creating ConcreteProduct objects.


Sequence diagram:

factory method Sequence diagram

  1. The client requests ConcreteFactory for the creation of ProductA .
  2. ConcreteFactory finds the concrete implementation of ProductA and creates a new instance.
  3. ConcreteFactory returns a new ConcreteProductA .
  4. The client requests ConcreteFactory for the creation of ProductB.
  5. ConcreteFactory finds the concrete implementation of ProductB and creates a new instance.
  6. ConcreteFactory returns a new ConcreteProductB .

there is nothing perfect so we have Tradeoffs for using the factory method:

- It can lead to a proliferation of factory classes.

- Adds complexity.

what about complex systems and architectures can we integrate it with other patterns?

yes, these Pattern works well with the factory method:

- Abstract Factory (more flexible, but more complicated)

- Singleton(if we needed only one instance of the object)

- Prototype (more flexible, but more complicated)

we will dive deeper into them later but this was an overview of the integration of the factory method with other design patterns.

in general Factory Method which is less complicated and more customizable via subclasses evolves toward Abstract Factory, Prototype, or Builder (more flexible, but more complicated) which we will talk about later.

if you want to delve deeper into understanding and implementing the factory method pattern check out these websites and books:

-Design Patterns Elements of Reusable Object-Oriented Software

-https://refactoring.guru/design-patterns/factory-method

-https://www.oodesign.com/factory-method-pattern

-https://reactiveprogramming.io/blog/en/design-patterns/factory-method



要查看或添加评论,请登录

Aws Ajwa的更多文章

  • Design patterns

    Design patterns

    saw this article by Arvind Saraswat and loved to share his abstract explanation of them: Design patterns are a way to…

社区洞察

其他会员也浏览了