Design Pattern 6: Prototype Pattern

Source: Internet
Author: User

1. My description

In my understanding, what is the prototype mode is to clone n objects. The cloned object should be exactly the same. Of course, you can modify the content of the cloned object. For C ++, the copy constructor is used. This mode is relatively simple, so it is no longer arrogant.

Ii. UML diagram

  

3. My code

#include<iostream>using namespace std;#include <string>class WorkExperience {public:    WorkExperience(string n) : name(n) {}    WorkExperience() {}    void SetName(string n) { name = n;}    string GetName() { return name; }    void SetDate(string d) { date = d; }    string GetDate() { return date; }    void SetCompany (string c) { company = c; }    string GetCompany() { return company; }private:    string name;    string date;    string company;};class Resume : public WorkExperience {public:    Resume (string n) : WorkExperience(n) {}    Resume (Resume& r) {        SetName(r.GetName());        SetDate(r.GetDate());        SetCompany(r.GetCompany());    }    Resume* Clone() {        return new Resume(*this);     }};int main(){    Resume *p_resume1 = new Resume("george");    p_resume1->SetDate("2003");    p_resume1->SetCompany("xxx");    Resume *p_resume2 = p_resume1->Clone();    p_resume2->SetDate("2004");    p_resume2->SetCompany("yyy");    cout << "Resume 1: " << p_resume1->GetName() << " " << p_resume1->GetDate() << " " << p_resume1->GetCompany() << endl;    delete p_resume1;    cout << "Resume 2: " << p_resume2->GetName() << " " << p_resume2->GetDate() << " " << p_resume2->GetCompany() << endl;    delete p_resume2;}

 

Design Pattern 6: Prototype Pattern

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.