Prototype pattern is called the prototype mode. In software systems, the creation of "some objects with complex structures" is often faced. due to changes in requirements, these objects are often subject to drastic changes, however, they have stable and consistent interfaces. Prototype is designed to address the problemProgram"Isolate" these variable objects ", so that" customer programs dependent on these variable objects "will not change as demand changes. The prototype design mode specifies the type of the object to be created, and then creates a new object by copying the prototype.
Key points:
- The prototype design mode is also used to isolate the coupling between the users of class objects and the specific types (variable classes). It also requires that these "variable classes" have "stable interfaces ".
- The prototype design mode uses the prototype clone method for "How to Create object of variable classes, it allows us to dynamically create new objects with certain stable interfaces with great flexibility. All we need is to register an object (prototype) of a new class ), and then constantly clone as needed
- Use deep clone when cloning.
Differences between deep copy and light copy: A shallow copy is also called a bit copy, which is a simple assignment between data members of an object, for example, a class does not have a specific definition of its copy constructor. When an object of this class is used to assign a value to an object, the execution process is a shortest copy. If the object does not contain other resources (such as heap, file, and system resources), there is no difference between deep copy and light copy, however, if there is such a small copy, it simply references these resources, and the deep copy will copy these resources. Example:
1 Class People 2 { 3 Public : 4 People (hair * Ha,Int A) 5 : H (HA), age () 6 {} 7 Private : 8 Hair * H; 9 Int Age; 10 }; 11 Hair * Ha =New Hair (....); 12 People p1 (HA, ); 13 People P2 = p1; // Shallow copy p2.h = p1.h, p2.age = p1.age In this case, the hair * H pointer is simply directed to the resource indicated by the HA pointer. Imagine that if we release the resources pointed to by p1.h when we destroy P1, an error will occur when we use P2 !! Therefore, we should try to use shallow copy as much as possible, which will bring harm to the program. Of course, we can also know from here that if there is no resource, there is no difference between the two copies. Next we will look at the prototype mode:
1 Class Wheel 2 { 3 Public : 4 Wheel (STD :: String N, Int L) 5 : Name (N), loadcapacity (l) 6 {} 7 Wheel () 8 : Name ( "" ), Loadcapacity ( 0 ) 9 {} 10 Void Show (); 11 Wheel * Clone (); 12 Private : 13 STD :: String Name; 14 Int Loadcapacity; 15 }; 16 Void Wheel: Show () 17 { 18 STD: cout < " The wheel name is " <Name <STD: Endl; 19 STD: cout < " The wheel loadcapacity is " <Loadcapacity < STD: Endl; 20 } 21 22 Wheel * Wheel: Clone () 23 { 24 Return New Wheel (name, loadcapacity ); 25 } 26 27 Class Car 28 { 29 Public : 30 Car (STD :: String Color, wheel * w, STD :: String Engine) 31 32 : _ Color (color), _ wheel (W), _ engine (engine) 33 {} 34 Virtual Car * clone () = 0 ; 35 Virtual Void Show () = 0 ; 36 Virtual Void Setcolor (STD :: String Color) = 0 ; 37 Virtual ~ Car (){} 38 Protected : 39 STD :: String _ Color; 40 // There is a resource 41 Wheel * _ Wheel; 42 STD :: String _ Engine; 43 }; 44 45 Class Benzcar: Public Car 46 { 47 Public : 48 Benzcar (STD :: String Color, wheel * w, STD :: String Engine) 49 : Car (color, W, engine) 50 {} 51 ~ Benzcar (); 52 Void Show (); 53 Car * Clone (); 54 Void Setcolor (STD :: String Color ); 55 }; 56 57 Benzcar ::~ Benzcar () 58 { 59 Delete _ wheel; 60 } 61 Car * Benzcar: Clone () 62 { 63 Wheel * l = _ Wheel. Clone (); 64 Car * Pc = New Benzcar (_ color, L, _ engine ); 65 Return PC; 66 } 67 Void Benzcar: Show () 68 { 69 STD: cout < " The car's color is " <_ Color < STD: Endl; 70 _ Wheel. Show (); 71 STD: cout < " The car's engine is " <_ Engine < STD: Endl; 72 } 73 74 Void Benzcar: setcolor (STD :: String Color) 75 { 76 _ Color = Color; 77 } 78 79 Int Main ( Int Argc, Char ** Argv) 80 { 81 Wheel * l = New Wheel ( " Michelin " , 160 ); 82 Benzcar BC ( " Red " , L, " Made in China " ); 83 STD: auto_ptr <car> PC (BC. Clone ()); 84 PC- Set ( " White " ); 85 PC->Show (); 86 BC-> Show (); 87 Return 0 ; 88 }
Running result: