原型模式,必须深拷贝啊


#include 
#include 
using namespace std;


class ICloneable{
public:
    ICloneable(){};
    virtual ~ICloneable(){};
    virtual ICloneable * Clone(){return NULL;};
};

class WorkExperience: public ICloneable {
private:
    string workData;
    string company;
public:
    WorkExperience(string workdata,string company):workData(workdata),company(company){};
    ~WorkExperience(){};
    
    string WorkData() {
        return workData;
    }
    
    void setWorkData(string workdata) {
        workData = workdata;
    }
    
    string Company() {
        return company;
    }
    
    void setCompany(string company) {
        this->company = company;
    }
    
    WorkExperience * Clone(){
        WorkExperience * e = new WorkExperience(workData,company);
        return e;
    };
};

class Resume:public ICloneable {
private:
    string name;
    WorkExperience *experience;
public:
    Resume(){}
    Resume(string name,string workdata,string company){
        this->name = name;
        experience = new WorkExperience(workdata,company);
    };
    
    string WorkData() {
        return experience->WorkData();
    }
    
    void setWorkData(string workdata) {
        experience->setWorkData(workdata);
    }
    
    string Company() {
        return experience->Company();
    }
    
    void setCompany(string company) {
        experience->setCompany(company);
    }
    
    Resume * Clone(){
        Resume * resume = new Resume();
        resume->name = name;
        WorkExperience * e = experience->Clone();
        (*resume).experience = e;
        return resume;
    }
    void printResume(){
        cout<< name<<"\t"<Company()<<"\t"<WorkData()<


void testLesson6() {
    Resume * resume = new Resume("zhang","100","akka");
    Resume * resume2 = resume->Clone();
    resume->setCompany("baidu");
    resume->printResume();
    resume2->printResume();
}

深拷贝没有什么多说的

你可能感兴趣的:(原型模式,必须深拷贝啊)