1. Pattern Intent
Defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses.
The factory method model is a further abstraction and generalization of the simple factory model .
Because of the use of polymorphism, the factory method pattern maintains the advantages of a simple factory model and overcomes its drawbacks.
In the factory method model, the core factory class is no longer responsible for the creation of all the products, but rather the specific creation work is given to subclasses. This core class is only responsible for giving the interfaces that a specific factory must implement without touching the details of which product class is instantiated. This allows the factory method model to allow the system to introduce new products without modifying the factory role.
In the factory method model, the factory class and the product class often have parallel hierarchies, with one by one corresponding to each other.
1.1 Factory Method mode V.s. Simple Factory mode
The core of the Factory method class is an abstract factory class, while the simple Factory mode core is placed on a specific class. The factory method pattern is also called Polymorphic Factory mode, because the concrete factories have the common interface, or have the common abstract class. When a system extension needs to add a new product object, only need to add a specific object and a specific factory object, the original factory object does not need to make any changes, and simple Factory mode after adding new product objects have to modify the factory method, scalability is not good. The factory method model can evolve into a simple factory model after degradation.
1.2 Abstract Factory mode V.s. Factory method Mode
Most abstract factory patterns are like this:
Inside it is a bunch of factory methods, and each factory method returns some type of object/product.
For example: The factory produces mouse and keyboard (product); Then the implementation classes of the abstract factory can generate the mouse and keyboard, for example a factory generates Logitech's Mouse and keyboard, and B factory produces Microsoft's mouse and keyboard, so that A and B correspond to the factory ; the mouse and keyboard produced by each factory are products, Corresponds to the factory method ;
Using the factory method pattern, replacing the factory method that builds the keyboard, you can switch the keyboard from Logitech to Microsoft. But using the abstract factory model, you can replace the mouse and keyboard at the same time as you change your factory, and if you have dozens of products, it is most convenient to replace all the products one at a time using abstract Factory mode.
abstract factories are like factories; factory methods are like a product line in a factory ;
The abstract factory model relies on the factory method model; The factory approach is a product hierarchy; The abstract factory pattern is oriented to multiple product hierarchies; The factory method pattern has only one abstract product class; The abstract factory class has a specific factory class for each abstract product class; , and the concrete factory class of the abstract factory pattern can create instances of multiple specific product classes;
2. Pattern definition
Abstract Factory Role (Creator): The core of the factory method pattern, independent of the application. Any factory class that creates objects in the schema must implement this interface;
Specific Factory (concrete Creator): This is a specific factory class that implements the abstract factory interface, including logic that is closely related to the application;
Abstract Product: The supertype of objects created by the factory method pattern, which is the common parent class of the Product object or the common-owned interface, such as light;
Specific product (concrete product): This role implements the interface defined by the abstract product; a specific product has a specific factory creation, which often corresponds to one by one;
3. Pattern definition
4. Pattern Application