avatarItchimonji

Summary

The Factory Method is a creational design pattern that enables the creation of objects with a common behavior but different specifications by deferring the instantiation process to subclasses, providing flexibility and ease of integration into systems.

Abstract

The Factory Method pattern is a fundamental design pattern that is widely recognized and utilized in scenarios where objects need to be created with uniform behavior yet varying details. This pattern achieves this by allowing subclasses to define the type of object to instantiate, thus offering a more extensible object version than direct object creation. It is recommended as a starting point for implementing creational patterns due to its adaptability, making it simple to incorporate into existing systems and to transition to other creational patterns if needed. The pattern is exemplified by an abstract creator class that delegates object creation to its subclasses, which can be seen in real-world examples such as an animal factory or a logistics factory.

Opinions

  • The Factory Method is suggested as the initial choice for a creational pattern when in doubt, due to its high flexibility and ease of integration.
  • It is emphasized that the Factory Method provides a way to abstract the creation process, which is beneficial for encapsulation and hiding the details of object creation.
  • The pattern is considered an important tool for developers, aiding in solving common programming problems and facilitating consistent communication about system design.
  • It is noted that the Factory Method pattern introduces developers to object composition and the principle of dependency inversion, which are key concepts in software development.
  • The use of the Factory Method pattern is seen as particularly useful for refactoring switch statements and promoting code reuse in frameworks like Angular.

Factory Method | Cheat Sheet

Creational Pattern — Design Patterns Series

The Factory Method is one of the most known Design Patterns and often used when creating things with same behavior, but with different specifications. This is made possible by delegation of the instantiation process to the specified subclass. This includes specialization options for subclasses, as this pattern provides an extended version of the object compared to the direct creation of the object.

If you want to use a Creational Pattern, but are unsure which one, you can start by implementing the Factory Method. Due to its high flexibility, the Factory Method is the easiest to integrate into the system and later you can switch to other Creational Patterns easily.

Real-life examples

  • Things with same behaviour, but different specifications: Animal Factory, Logistics Factory (delivery method or transport vehicle)

Meaning

  • Abstract interface for object creation, where the determination of the class to be instantiated is left to the subclass
  • Delegation of instantiation to a subclass

Applicability

  • Classes to be created cannot be determined in advance, therefore an abstract interface is provided which can be reached via a function
  • A class expects from its subclasses a specification of the products to be created

Assets and Drawbacks

  • Specialization options for subclasses, as this pattern provides an extended version of the object compared to the direct creation of the object

Best Practises

  • If you want to use a Creational Pattern, but are unsure which one, you can start by implementing the Factory Method
  • Later you can switch to other Creational Patterns
  • Due to its high flexibility the Factory Method is the easiest to integrate into the system

Example

First of all, you need to define a product. For this example I chose types of food.

Product classes

After that, you can define the abstract Creator and its subclasses to create some pizzas and wraps.

Abstract Creator class

The client could consume the Factory Method as follows.

Client code

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and are a significant introduction into object composition (besides inheritance) and dependency inversion.

Creational Patterns are indispensable when abstracting the instantiation process of objects. Of high importance are encapsulation of concrete classes and hiding the creational process.

GitHub

Learn More

Resources

Design Patterns
Factory Method
Object Composition
Cheatsheet
Creational Patterns
Recommended from ReadMedium