Prototype mode (PROTOTYPE): Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes. The prototype pattern is actually creating another customizable object from one object without needing to know the specifics of the creation.
#ifndef prototype_h#define prototype_h#include<iostream> #include <string>using namespace Std;class Resume{string name;string sex;string age;string timearea;string company;public:resume () {}~Resume () {cout << name << "<< sex <<" "<< Age << endl;cout <<" workexperience: "<< timearea <& Lt "<< company <<" destory!\n\n ";} Resume (string N): Name (n) {}resume (Resume & N); void Setpersonalinfo (String sex, String age) {this->sex = sex; this- >age = age; }void setworkexperience (string T, string com) {This->timearea = T; this->company = com;} void Display () const; Resume Clone () const;void operator = (Resume & res);}; void Resume::D isplay () const{cout << name << "<< sex <<" "<< Age << Endl;cout < < "workexperience:" << timearea << "<< company << Endl;} Resume resume::clone () const{resume Temp (name); Temp.age = age; Temp.sex = sex; Temp.cOmpany = Company; Temp.timearea = Timearea; Temp.name = Name;return Temp;} void Resume::operator = (Resume & res) {age = Res.age;sex = Res.sex;company = Res.company;timearea = Res.timearea;} Resume::resume (Resume & N) {if (this = = &n) Return;this->age = N.age;this->company = n.company;this-> Timearea = N.timearea;this->sex = N.sex;this->name = N.name;} #endif
#include "Prototype.h" int main () {Resume A ("Bigbird"), A.setpersonalinfo ("Man", "" ") a.setworkexperience (" 1998-2000 "XX company"); A.display ();//using the Clone function there will be an intermediate Resume object generated, the destructor will appear two A of the destruction//resume B = A.clone ();//b.setworkexperience ( "1998-2006", "yy Company"),//b.display ();//Using constructors does not have an intermediate resume object, and there is only one destructor for a in the destructor, and a destructor for a after C is refactored (a); C. Setpersonalinfo ("Man", "" "); C.display (); return 0;}
Design pattern C + + implementation six: Prototype mode