Part 0 "Object creation" mode
Bypassing new in object creation mode avoids tight coupling (relying on specific classes) caused by object creation (new), thus supporting the stability of object creation. It is the first step after the interface abstraction.
Typical mode
Factory Method
Abstract Factory
Prototype
Builder
Part 1 Factory Method factory methods
Motive (motivation)
In software systems, it is often the work of creating objects, and the specific types of objects that need to be created often change due to changes in requirements.
How to deal with this change? How do you bypass the general object creation method (new) and provide a "encapsulation mechanism" to avoid tight coupling between client programs and this "concrete object creation work"?
Pattern definition
Defines an interface for creating objects, letting subclasses decide which class to instantiate. Factory method enables the instantiation of a class with a delay (purpose: Decoupling, means: virtual function) to subclasses. --"Design pattern" GoF
Uml
Summary of Points
The Factory method pattern is used to isolate the coupling between the consumer and the specific type of the class object. In the face of a specific type that is constantly changing, tight coupling relationships (new) can lead to software fragility.
The Factory method mode, by means of object-oriented approach, defers the work of the concrete object to be created to the subclass, thus implementing a strategy of extension (not change), which solves the tight coupling relationship better.
Factory method mode solves the requirement change of "single object". The disadvantage is that the creation method/parameter is required to be identical.
"Object creation" mode of C + + design mode: Factory method