I. Concepts
The factory method mode defines a factory interface for creating a product object and delays the actual creation to the subclass. The core factory category is no longer responsible for product creation. In this way, the core class becomes an abstract factory role and is only responsible for the interfaces that must be implemented by specific factory subclass, the advantage of further abstraction is that the factory method mode enables the system to introduce new products without modifying the specific factory role.
The factory method model is derived from the simple factory model and solves many problems of the simple factory model. First, the 'open-close policy' is fully implemented to achieve scalability. Secondly, a more complex hierarchy can be applied to scenarios where product results are complex.
II. Key Points
- Abstract Factory role: the Core of the factory method Model and Its ApplicationProgramIrrelevant. Any factory class of the object created in the mode must implement this interface.
- Concrete Creator: this is a specific factory class that implements Abstract Factory interfaces. It contains the logic closely related to applications and is called by applications to create product objects. There are two such roles: bulbcreator and tubecreator.
- Abstract Product role: the super type of the object created in the factory method mode, that is, the common parent class of the product object or the common interfaces. In, this role is light.
- Specific product role: this role implements the interface defined by the abstract Product role. A specific product is created in a specific factory, which is usually one-to-one.
Iii. Scenarios
- The first case is that the caller knows exactly which factory service should be used for a product, instantiate the specific factory, and produce a specific product. The iterator () method in the Java Collection belongs to this situation.
- In the second case, only one product is needed, and you do not need to know which factory is for production, that is, the decision on which specific factory is used is decided by the producer, they instantiate a specific factory based on the current system and return it to the user, which is transparent to the user.