Prototype Mode C + + implementation 1 definition
Specify the kind of objects created with the prototype instance and create new objects by copying the prototypes
2 class Diagram
3 implementation
Class Prototype
{
Protected
Prototype ();
Public
Virtual prototype* Clone () const = 0;
Vitual ~prototype () = 0;
};
Class Prototypeclass:public Prototype
{
Public
Prototypeclass ();
Prototypeclass (const PROTOTYPECLASS & CP);
prototype* Clone () const
{return 0;}
};
Note: The key to the prototype model in C + + is the implementation of the copy constructor.
Factory mode, builder mode, and prototype mode all construct new objects through existing class objects, except
Factory mode: Focus on mass production of multiple interdependent classes of objects
Builder mode: Step-up creation with emphasis on complex objects
Prototype mode: Implement new object construction by copying itself
4 applications
Advantages
Good performance, avoid the constraints of the constructor function.
5 Usage Scenarios
① Resource optimization scenario, class initialization requires a large resource (data, hardware)
② Performance and security requirements, data preparation and permissions issues with new frequent production
③ an object with multiple modified scenes, eg student Information form.
6 precautions
Constructor will not be executed
A deep copy problem, when the inside of a class contains pointers, it is necessary to implement a dark copy yourself.
Design pattern--Prototype mode C + + implementation