Home >Common Problem >What does factory method pattern mean?
The factory method pattern is a commonly used class creation design pattern. The core spirit of this pattern is to encapsulate the changing parts of the class, extract the personalized and changeable parts into independent classes, and achieve this through dependency injection. The purpose is to decouple, reuse and facilitate later maintenance and expansion.
Pattern introduction:
The meaning of the Factory Method (Factory Method) pattern is to define a factory that creates product objects Interface defers the actual creation to subclasses. The core factory class is no longer responsible for the creation of products. In this way, the core class becomes an abstract factory role, responsible only for the interfaces that specific factory subclasses must implement. The benefit of further abstraction is that the factory method pattern allows the system to operate without modifying the specific factory role. Introducing new products.
The factory method pattern is a derivative of the simple factory pattern and solves many problems of the simple factory pattern. First of all, the ‘open-close principle’ is fully realized and scalable. Secondly, more complex hierarchical structures can be applied to situations where the product results are complex. [2]
The factory method pattern abstracts the simple factory pattern. There is an abstract Factory class (can be an abstract class and an interface). This class will no longer be responsible for specific product production, but will only formulate some specifications, and the specific production work will be completed by its subclasses. In this model, factory classes and product classes can often correspond in sequence. That is, an abstract factory corresponds to an abstract product, and a concrete factory corresponds to a specific product. This specific factory is responsible for producing the corresponding product.
Factory Method pattern is the most typical application of Template Method pattern.
Role structure:
Abstract Factory (Creator) role: It is the core of the factory method pattern and has nothing to do with the application. Any factory class for objects created in the pattern must implement this interface.
Concrete Factory (Concrete Creator) role: This is a concrete factory class that implements the abstract factory interface, contains logic closely related to the application, and is called by the application to create product objects. There are two such roles in the picture above: BulbCreator and TubeCreator.
Abstract Product (Product) role: The super type of the object created by the factory method pattern, which is the common parent class or commonly owned interface of the product object. In the picture above, this character is Light.
Concrete Product role: This role implements the interface defined by the abstract product role. A specific product is created in a specific factory, and there is often a one-to-one correspondence between them.
Pattern application:
Factory methods are often used in the following two situations:
The first situation is for a certain product , the caller clearly knows which specific factory service should be used, instantiates the specific factory, and produces specific products. This is the case with the iterator() method in Java Collection.
In the second case, you just need a product, and you don’t want to know or need to know which factory is producing it. That is, the final decision on which specific factory to use lies with the producer. They are based on the current system. According to the situation, a specific factory is instantiated and returned to the user, and this decision-making process is transparent to the user.
The above is the detailed content of What does factory method pattern mean?. For more information, please follow other related articles on the PHP Chinese website!