Prototype: specifies the type of the object to be created using the prototype instance, and creates a new object by copying the prototype.
Is the structure diagram of the prototype mode:
The prototype model is actually an object that creates another customizable object without any details. Let's look at the basic prototype code.
// Prototype class prototype {PRIVATE: String ID; public: Prototype (string ID) {This-> id = ID;} string GETID () {return ID ;} virtual Prototype * clone () = 0 ;}; // The specific prototype class concreteprototype: Public prototype {public: concreteprototype (string ID): Prototype (ID) {} prototype * clone () {return New concreteprototype (* This );}};
Generally, cloning is the best method when the initialization information does not change. This not only hides the details of object creation, but also greatly improves the performance. That is, you do not need to reinitialize the object, but dynamically obtain the status of the object during running.
The key to the prototype system is to implement the clone function. Of course, this involves the concept of shortest copy and deep copy. However, the copy constructor must implement deep copy. Before using deep copy to implement the resume, let's talk about these two copies. In light replication, all variables of the Copied object contain the same value as the original one, and all references to other objects still point to the original object. Deep replication points the variables of the referenced object to the Copied object instead of the original referenced object.
Deep copy of resumes:
# Include <iostream> using namespace STD; Class iclonable {public: Virtual void setpersonalinfo (string sex, string age) = 0; virtual void setworkexperience (string workdate, string Company) = 0; virtual void display () = 0; virtual iclonable * clone () = 0 ;}; class workexperience: Public iclonable {PRIVATE: String workdate; string company; public: workexperience () {} void setpersonalinfo (string sex, string age) {} void display () {} void setworkexperience (string workdate, string Company) {This-> workdate = workdate; this-> Company = company;} string getworkdate () {return workdate;} string getcompany () {return company;} iclonable * clone () {workexperience * TMP = new workexperience (); TMP-> setworkexperience (workdate, company); Return TMP ;}}; class resume: Public iclonable {PRIVATE: string name; string sex; string age; workexperience * work; resume (workexperience * Work) {// provides a private constructor called by the clone method, this allows you to clone the data of a "work experience". This-> Work = (workexperience *) Work-> clone ();} public: Resume (string name) {This-> name = Name; work = new workexperience ();} void setpersonalinfo (string sex, string age) {This-> sex = sex; this-> age = age;} void setworkexperience (string workdate, string Company) {work-> setworkexperience (workdate, company);} void display () {cout <name <"" <sex <"" <age <Endl; cout <"work experience:" <work-> getworkdate () <"" <work-> getcompany () <Endl;} iclonable * clone () {// call a private constructor to clone the work experience, then assign a value to the relevant field of the "resume" object, and finally return a deeply copied resume object resume * OBJ = New resume (this-> work ); OBJ-> name = This-> name; obj-> sex = This-> sex; obj-> age = This-> age; return OBJ ;}; int main () {resume * A = New resume (""); A-> setpersonalinfo ("male", "29"); A-> setworkexperience ("1998-2000 ", "XX Company"); resume * B = (resume *) A-> clone (); B-> setworkexperience ("1998-2006", "YY enterprise "); resume * c = (resume *) A-> clone (); C-> setpersonalinfo ("male", "24"); C-> setworkexperience ("1998-2003 ", "ZZ enterprise"); A-> display (); B-> display (); C-> display (); Return 0 ;}