《大话设计模式》
原型模式:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新饿对象
下面的例子是通过一个对象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;
}
输出结果跟上面的一样