Factory method pattern (study notes)

1. Concept

Design patterns are mainly divided into three types: (1) creation-type patterns; (2) structural patterns; (3) behavioral patterns. Among them, the factory method mode is a creation mode. The following is the definition of the factory method pattern:

Define an interface for creating objects, let subclasses decide which class to instantiate, Factory Method delays the instantiation of a class to its subclasses.

The characteristic of the factory method pattern is to let the parent class complete the creation of the product interface object without knowing the specific implementation subclass of the product interface , and the specific implementation selection is delayed to the subclass of the factory method class . The essence of the factory method is to delay to the subclasses of the factory method class to select the "product subclass objects required by the product interface". When the factory method in the factory method pattern is actually implemented, it is usually first to choose which specific product implementation class to use, use this class to create a specific product implementation object , and then return it to the client (caller). In other words, the factory method itself does not implement the product interface. The specific product selection implementation has been completed by the subclass of the factory method. The factory method only needs to select the factory method subclass to create a specific product object that implements the product interface .

note:

In the implementation of factory methods, usually the parent class will be an abstract class, which contains abstract methods for creating the required objects. These abstract methods are factory methods. When subclasses implement these abstract methods, they usually do not really implement specific functions by subclasses, but instead make choices in subclass methods and choose specific product implementation objects.

2. Structure and description

The structure of the factory method is shown in the figure below.
Insert picture description here

  • Product: Define the interface of the object created by the factory method, that is, the interface of the object that actually needs to be used.
  • ProductImpl1, ProductImpl2: Implementation objects of the specific Product interface.
  • Creator: Creator, declares factory methods, factory methods usually return an instance object of Product type, and most of them are abstract methods. You can also provide a default implementation of the factory method in Creator, so that the factory method returns a default instance object of the Product type.
  • CreatorImpl1, CreatorImpl2: Specific creator objects, which override the factory method defined by the Creator, and return specific instances of Product subclasses (classes that implement interfaces).

3. Relationship with Simple Factory

The following picture shows the factory method pattern
Insert picture description here

The following figure shows the simple factory model
Insert picture description here
through comparison and found that the essence of the factory method is also the implementation of choice. The difference from the simple factory is that the factory method model delays the selection of specific implementation functions to subclasses.
If the implementation selected in the factory method pattern is placed in the parent class to directly select the implementation, it will be reduced to a simple factory. Conversely, in the simple factory model, if the selected specific implementation functions are implemented in its subclasses, it will evolve into a factory model.

4. Detailed mode

The factory method pattern can be programmed without knowing the specific implementation of the product object. When implementing functions, if you need a product implementation object, you only need to use the product object's interface, and you don't need to care about the specific implementation. The task of selecting a specific implementation is delayed to the subclass to complete. The factory method provides a hook for the factory method subclass, which makes it very easy to extend new object versions, and also reflects the OCP principle of object-oriented design that is open for extension and closed for modification .

5 Conclusion

Just learning design patterns, the feeling that design patterns give me is that when I look at design patterns, I seem to understand. But once I put down the book, or related reference materials, I almost forgot it again. Therefore, the purpose of this and subsequent writing design pattern study notes is to provide a condensed version of the design pattern for relevant learners to refer to and remember. By recording important knowledge, or extracting it, and writing down your own understanding at the same time, you can become familiar with the relevant patterns and facilitate memory, which will help you understand the relevant patterns in depth. This time the focus is to truly digest the basic concepts, and then go into the details later.

6. Reference Resources

"Grinding Design Mode" Chen Chen, Wang Bin, Tsinghua University Press, 2011.5

Guess you like

Origin blog.csdn.net/fanjianglin/article/details/112444453