原型模式

《大话设计模式》

原型模式:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新饿对象

下面的例子是通过一个对象re来进行拷贝,但是指向的都是同一个对象的引用,改变克隆后的属性,原来的原型属性也改变,所以是浅复制,后面再实现一个深度复制

 

  //examination.h
#ifndef _EXAMINATION_H_
#define _EXAMINATION_H_

#include <iostream>
#include <string>
using namespace std;

class ICloneable
{
    public:
        ICloneable()
        {
            cout << "In ICloneable's construction" << endl;
        }
        
        virtual ~ICloneable()
        {
            cout << "In ICloneable's desconstruction" << endl;
        }
        
        virtual ICloneable *clone() = 0;
        virtual void Display() = 0;
};

class Resume: public ICloneable
{
    public:
        Resume()
        {
            cout << "In Resume's construction" << endl;
        }
        
        Resume(string na):name(na)
        {
            cout << "In Resume's construction" << endl;
        }
        
        ~Resume()
        {
            cout << "In Resume's desconstruction" << endl;
        }
        
        void SetPersonalInfo(string sex, string age)
        {
            this->age = age;
            this->sex = sex;
        }
        
        void SetWorkExperience(string timeArea, string company)
        {
            this->timeArea = timeArea;
            this->company = company;
        }
        
        void Display()
        {
            cout << "姓名:" << name << " 性别:" << sex << " 年龄:" << age << endl;
            cout << "工作时间:" << timeArea << " 公司:" << company << endl;
        }
 
	Resume *clone()
        {
            return this;
        }
    private:
        string name;
        string sex;
        string age;
        string timeArea;
        string company;
};

#endif


 

#include <iostream>
#include <cstdio>
#include "examination.h"
using namespace std;

int main()
{
   
    Resume *re = new Resume("大鸟");
    re->SetPersonalInfo("男", "26");
    re->SetWorkExperience("2000-2005", "xx公司");
    re->Display();
    Resume *cl = re->clone();
    cl->SetWorkExperience("2005-2010", "yy企业");
    cl->Display();
    re->Display();
    delete re;
    re = NULL;
    cl = NULL;
      return 0;
}



输出:

In ICloneable's construction
In Resume's construction
姓名:大鸟 性别:男 年龄:26
工作时间:2000-2005 公司:xx公司
姓名:大鸟 性别:男 年龄:26
工作时间:2005-2010 公司:yy企业
姓名:大鸟 性别:男 年龄:26
工作时间:2005-2010 公司:yy企业
In Resume's desconstruction
In ICloneable's desconstruction

 

深度复制,在clone里面返回一个新的对象,然后把属性值复制

//examination.h
#ifndef _EXAMINATION_H_
#define _EXAMINATION_H_

#include <iostream>
#include <string>
using namespace std;

class ICloneable
{
    public:
        ICloneable()
        {
            cout << "In ICloneable's construction" << endl;
        }
        
        virtual ~ICloneable()
        {
            cout << "In ICloneable's desconstruction" << endl;
        }
        
        virtual ICloneable *clone() = 0;
        virtual void Display() = 0;
};

class Resume: public ICloneable
{
    public:
        Resume()
        {
            cout << "In Resume's construction" << endl;
        }
        
        Resume(string na):name(na)
        {
            cout << "In Resume's construction" << endl;
        }
        
        ~Resume()
        {
            cout << "In Resume's desconstruction" << endl;
        }
        
        void SetPersonalInfo(string sex, string age)
        {
            this->age = age;
            this->sex = sex;
        }
        
        void SetWorkExperience(string timeArea, string company)
        {
            this->timeArea = timeArea;
            this->company = company;
        }
        
        void Display()
        {
            cout << "姓名:" << name << " 性别:" << sex << " 年龄:" << age << endl;
            cout << "工作时间:" << timeArea << " 公司:" << company << endl;
        }
        
        Resume *clone()
        {
            Resume *tmp = new Resume(this->name);
            tmp->SetPersonalInfo(this->sex, this->age);
            tmp->SetWorkExperience(this->timeArea, this->company);
            return tmp;
        }
        
    private:
        string name;
        string sex;
        string age;
        string timeArea;
        string company;
};

#endif


 

#include <iostream>
#include <cstdio>
#include "examination.h"
using namespace std;

int main()
{
       Resume *re = new Resume("大鸟");
    re->SetPersonalInfo("男", "26");
    re->SetWorkExperience("2000-2005", "xx公司");
    Resume *cl = re->clone();
    re->Display();
    cl->Display();
    cl->SetWorkExperience("2005-2010", "yy企业");
    re->Display();
    cl->Display();
    delete re;
    delete cl;
    re = NULL;
    cl = NULL;
    
    return 0;
}


 

输出结果:

In ICloneable's construction
In Resume's construction
In ICloneable's construction
In Resume's construction
姓名:大鸟 性别:男 年龄:26
工作时间:2000-2005 公司:xx公司
姓名:大鸟 性别:男 年龄:26
工作时间:2000-2005 公司:xx公司
姓名:大鸟 性别:男 年龄:26
工作时间:2000-2005 公司:xx公司
姓名:大鸟 性别:男 年龄:26
工作时间:2005-2010 公司:yy企业
In Resume's desconstruction
In ICloneable's desconstruction
In Resume's desconstruction
In ICloneable's desconstruction

其实感觉达到这个原型模式的效果,并且是深度复制的话,用类的复制构造函数一样能实现这样的效果

 

//examination.h
#ifndef _EXAMINATION_H_
#define _EXAMINATION_H_

#include <iostream>
#include <string>
using namespace std;

class ICloneable
{
    public:
        ICloneable()
        {
            cout << "In ICloneable's construction" << endl;
        }
        
        virtual ~ICloneable()
        {
            cout << "In ICloneable's desconstruction" << endl;
        }
        
        virtual ICloneable *clone() = 0;
        virtual void Display() = 0;
};

class Resume: public ICloneable
{
    public:
        Resume()
        {
            cout << "In Resume's construction" << endl;
        }
        
        Resume(string na):name(na)
        {
            cout << "In Resume's construction" << endl;
        }
        
        Resume(const Resume &re):ICloneable(re), name(re.name), sex(re.sex), age(re.age), timeArea(re.timeArea), 

company(re.company)
        {
            cout << "In Resume's construction" << endl;
        }
        
        Resume &operator = (const Resume &re)
        {
            if(this != &re)
            {
                ICloneable::operator=(re);
                name = re.name;
                age = re.age;
                sex = re.sex;
                timeArea = re.timeArea;
                company = re.company;
            }
            return *this;
        }
        
        ~Resume()
        {
            cout << "In Resume's desconstruction" << endl;
        }
        
        void SetPersonalInfo(string sex, string age)
        {
            this->age = age;
            this->sex = sex;
        }
        
        string getName()
        {
            return this->name;
        }
        
        string getAge()
        {
            return this->age;
        }
        
        string getSex()
        {
            return this->sex;
        }
        
        string getTimeArea()
        {
            return this->timeArea;
        }
        
        string getCompany()
        {
            return this->company;
        }
        
        void SetWorkExperience(string timeArea, string company)
        {
            this->timeArea = timeArea;
            this->company = company;
        }
        
        void Display()
        {
            cout << "姓名:" << name << " 性别:" << sex << " 年龄:" << age << endl;
            cout << "工作时间:" << timeArea << " 公司:" << company << endl;
        }
        
        Resume *clone()
        {
            Resume *tmp = new Resume(this->name);
            tmp->SetPersonalInfo(this->sex, this->age);
            tmp->SetWorkExperience(this->timeArea, this->company);
            return tmp;
        }
        
    private:
        string name;
        string sex;
        string age;
        string timeArea;
        string company;
};

#endif

#include <iostream>
#include <cstdio>
#include "examination.h"
using namespace std;

int main()
{
   
    Resume re("大鸟");
    re.SetPersonalInfo("男", "26");
    re.SetWorkExperience("2000-2005", "xx公司");
    Resume cl(re);
    re.Display();
    cl.Display();
    cl.SetWorkExperience("2005-2010", "yy企业");
    re.Display();
    cl.Display();
    
    
    return 0;
}


输出结果跟上面的一样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值