Prototype
Purpose:
Specify the kinds of objects to create using a prototypical instance, and create
New objects by copying this prototype.
Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
Key Points of C ++ implementation:
1. In prototype mode, the client does not know the actual type of the object to be cloned. You only need to know the base class type. 2. The advantage of cloning an object over directly creating an object is that cloning brings the behavior attributes of the original object to the new object. 3. c ++ has no cloning method. To clone an object, you must use the copy constructor. In the copy constructor, the object to be copied has a shortest copy and a deep copy: when the object is copied, only the value of the data member in the object is copied; when the object is assigned a value, copy the values of simple data members of an object, and allocate dynamic space for dynamic members (heap or other system resources) of the object. When the class does not define a copy constructor, the compiler automatically generates a constructor called the default copy constructor. By default, the copy constructor uses the shortest COPY method. If the class contains dynamic data members, you must use the deep COPY method to implement the copy constructor. Otherwise, when the object is destroyed, the destructor of the two objects will release the same memory twice, resulting in a runtime error.
// Prototype. h # include <string> using namespace STD; Class prototype {public: Virtual Prototype * clone () = 0; virtual void show () = 0 ;}; class concreteprototype1: public prototype {PRIVATE: String m_str; public: concreteprototype1 (); concreteprototype1 (string CSTR); concreteprototype1 (const concreteprototype1 & C); Virtual Prototype * clone (); virtual void show () ;}; class concreteprototype2: Public prototype {PRIVATE: String m_str; public: concreteprototype2 (string CSTR); concreteprototype2 (const concreteprototype2 & C ); virtual Prototype * clone (); Virtual void show () ;}; // prototype. CPP # include "prototype. H "# include <iostream>/* using namespace STD; concreteprototype1: concreteprototype1 () {cout <" default the concreteprototype1 "<Endl; this-> m_str = "Default type1";} concreteprototype1: concreteprototype1 (string CSTR) {cout <"construct the concreteprototype1" <Endl; this-> m_str = CSTR ;} concreteprototype1: concreteprototype1 (const concreteprototype1 & C) {cout <"copy the concreteprototype1" <Endl; this-> m_str = C. m_str;} prototype * concreteprototype1: Clone () {cout <"clone the concreteprototype1" <Endl; return New concreteprototype1 (* This);} void concreteprototype1: Show () {cout <"show the concreteprototype1" <Endl; cout <this-> m_str <Endl;} concreteprototype2: concreteprototype2 (string CSTR) {This-> m_str = CSTR;} concreteprototype2: concreteprototype2 (const concreteprototype2 & C) {cout <"copy the concreteprototype2" <Endl; this-> m_str = C. m_str;} prototype * concreteprototype2: Clone () {return New concreteprototype2 (* This);} void concreteprototype2: Show () {cout <this-> m_str <Endl;} // main. CPP # include "prototype. h "void main () {concreteprototype1 CP1; concreteprototype1 CP2 = CP1; // the original copy constructor cp1.show (); cp2.show (); prototype * CP3 = new concreteprototype1 ("concreteprototype1"); // the pointer type is parent class CP3-> show (); prototype * cp4 = CP3-> clone (); cp4-> show (); Delete CP3; CP3 = NULL; cp4-> show (); return ;}
View code
References
1 design pattern C ++ description ---- 08. Prototype Pattern
2 C ++ design mode-Prototype
3 C ++ design mode-Prototype
4. design patterns I understand (C ++ implementation) -- prototype Pattern)
Prototype of Design Pattern