Problem description
See this pattern, it is easy to think of childhood to see the "Journey to the World", Qi, the King of the Sun Wukong can be the head of the 3 hair immediately copied out tens of thousands of Monkey King, to deal with the small monster is very useful (the number of most important).
The Prototype mode also provides self-replicating functionality, meaning that the creation of new objects can be created with existing objects. In C + +, the copy constructor (copy Constructor) was once a nightmare for programmers, and the magic dire of shallow and deep copies was one of the root causes of the fast food and system crashes of many programmers during interviews.
In Gof's design pattern: The basics of reusable object-oriented software, this is said: use prototype instances to specify the kind of objects that are created, and create new objects by copying those prototypes . In this definition, the most important word is "copy", which is the verbal copy, and this copy, which is the essence of the prototype pattern.
UML Class Diagram
Since cloning requires a prototype, and the above class diagram prototype this prototype, prototype defines the clone's own clone interface, which is implemented by the derived class, and the key to implementing the prototype pattern is the implementation of the Clone interface. The ConcretePrototype1 class and the ConcretePrototype2 class inherit from the prototype class and implement the Clone interface to implement the cloning operation; In the ConcretePrototype1 class and the ConcretePrototype2 class, you need to override the default copy constructor for the clone function call, and clone is implemented by calling the overridden copy constructor internally. In the subsequent encoding process, if a class needs to implement the Clone function, it is only necessary to inherit the prototype class and then rewrite its own default copy constructor. As with the ICloneable interface provided in C #, when a class needs to implement a prototype schema, the only way to implement this interface is the same.
Use occasions
Prototype mode is one of the creation patterns, as is the builder mode and factory method pattern. In a nutshell, we use prototype mode to create objects. However, the best choice for prototype mode is as follows:
1. It is easier to clone a new object by this type of object when our object type is not initially determined, and this type is determined at run time.
2. Sometimes, we need a copy of an object in a certain state, at this point, we use the prototype mode is the best choice, for example: An object, after a period of processing, the state of its internal changes; this time, we need a copy of this state, if it is a new object directly, But its state is not right, at this time, you can use the prototype mode, the original object is copied out, the object is exactly the same as the previous object;
3. When we are dealing with some relatively simple objects, and the difference between objects is very small, may be a few properties are different, then you can use the prototype mode to complete, eliminating the trouble of creating objects;
4. Sometimes, when creating an object, the constructor has a lot of arguments, and you do not fully know the meaning of each parameter, you can use the prototype pattern to create a new object, do not have to ignore the process of creation.
Appropriate time to consider the prototype model, can reduce the corresponding workload, reduce the complexity of the program, improve efficiency.
Code implementation
#include <iostream> #include <string>using namespace Std;class prototype{private:string str;public: Prototype (string s) {str = s;} Prototype () {str = "";} void Show () {cout << str << Endl;} Virtual Prototype *clone () = 0;}; Class ConcretePrototype1:p ublic prototype{public:concreteprototype1 (string s):P Rototype (s) {}concreteprototype1 () { }virtual Prototype *clone () {ConcretePrototype1 *p = new ConcretePrototype1 (); *p = *this;return p;}}; Class ConcretePrototype2:p ublic prototype{public:concreteprototype2 (string s):P Rototype (s) {}concreteprototype2 () { }virtual Prototype *clone () {ConcretePrototype2 *p = new ConcretePrototype2 (); *p = *this;return p;}}; int main () {ConcretePrototype1 *test = new ConcretePrototype1 ("Xiao Li"); ConcretePrototype2 *test2 = (ConcretePrototype2 *) Test->clone (); Test->show (); Test2->show (); return 0;}
Operation Result:
#include <iostream> #include <string>using namespace std; Class Resume{private:string Name,sex,age,timearea,company;public:resume (string s) {name=s;} void Setpersonalinfo (String s,string a) {sex=s;age=a;} void Setworkexperience (String t,string c) {timearea=t;company=c;} void display () {cout<<name<< " " <<sex<< " " <<age<<endl;cout<< " Work experience: "<<timeArea<<" "<<COMPANY<<ENDL<<ENDL;} Resume *clone () {Resume *b;b=new resume (name); B->setpersonalinfo (sex,age); B->setworkexperience (Timearea, Company); return b;}; int main () {Resume *r=new resume ("Li Junhong"); R->setpersonalinfo ("Male", "n"), R->setworkexperience ("2007-2010", "graduate student"); R->display (); Resume *r2=r->clone (); R2->setworkexperience ("2003-2007", "undergraduate"); R->display (); R2->display (); return 0;}
Operation Result:
As always recommended:C + + design mode-prototype mode
Prototype mode of design mode (c + +)