Factory method (Factory method mode, virtual constructor mode, and multi-state factory mode ):
Defines the interface for object creation, and the derived class determines the class to create the instance. That is, the factory method delegates (delays) the responsibility of the created object to the creation of the derived class.
Applicability:
1. When a class cannot know the class of the object she must create in advance.
2. When a class needs its derived class to decide to build an object.
3. When the class delegates the responsibility of creating an object to one of the multiple Derived classes that assist it, and you want to centrally manage the object creation logic through the delegate Derived classes.
This mode separates the object from the object creation method, especially when the object contains complex creation logic, this separation allows the logic of creating an object to be removed from the object and encapsulated in a factory class. By redefining an abstract interface (abstract class and object interface), you can manage specific factories that encapsulate different creation logics. As long as an application system calls the factory method provided by the interface, the derived class can build the desired product object without knowing the class of these objects.
Mode structure:
1. Abstract product: defines the interface of the product object built by the factory method. Is the public base class or common interface of a specific product class.
2. A specific product (cconcreteproduct) is an interface for implementing abstract product objects. It is also the final product instance created by the factory method.
3. Abstract Factory: Declares the factory method, which returns the objects of a specific product. (Mode core) creates a product object by calling a factory method of the abstract factory.
4. cconcretefactory: it is a specific class that implements the abstract factory interface. it overwrites the factory method of the factory and returns the instance of the specific product.
UML diagram: Click Back (sorry ). It has not been painted.
Code Template: Go back. Haha
// Because of the speed at which a person can draw a picture, all he sees is text. You have time to attach the image. As for the template code, I will package it back in the form of C ++ source code. It is being sorted out. Due to the busy schedule, I am not sure, but I will try my best. Haha