Design pattern C + + implementation six: Prototype mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.