原型模式提供了自我复制的功能,就是说新对象的创建可以通过已有的对象进行创建。用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。其中有一个词很重要,那就是拷贝。可以说,拷贝是原型模式的精髓所在。
举个例子来介绍原型模式,在找工作的时候,我们需要准备简历,假设没有打印设备,我们需要手写多份简历,这些简历的内容都是一样的,这样有个缺陷,如果想要修改简历中的某项,那么所有已经写好的简历都需要修改,工作量很大,随着科技的进步,出现了打印设备。我们只需要手写一份,然后利用打印设备复印多份即可。如果要修改简历中的某项,那么修改原始版本就可以,然后再复印。原始的那份手稿相当于一个原型,有了它,通过复印(拷贝)创造出更多的新简历。这就是原型模式的基本思想。
原型模式实现的关键就是Clone函数,对于C++来说,其实就是拷贝构造函数,需实现深拷贝。
#include<iostream>
#include<string>
using namespace std;
//父类
class Resume
{
protected:
char *name;
public:
Resume(){
}
virtual ~Resume(){
}
virtual Resume* Clone(){
return NULL;}
virtual void Set(const char *n){
}
virtual void Show(){
}
};
class ResumeA :public Resume
{
public:
ResumeA(const char* str); //构造函数
ResumeA(const ResumeA& r); //拷贝构造函数
~ResumeA(); //析构函数
ResumeA* Clone(); //克隆 关键所在
void Set(const char *n);
void Show();
};
ResumeA::ResumeA(const char *str)
{
if(str == NULL){
name = new char[1];
name[0] = '\0';
}
else
{
name = new char[strlen(str)+1];
strcpy(name,str);
}
}
ResumeA::~ResumeA()
{
delete []name;
}
ResumeA::ResumeA(const ResumeA& r)
{
name = new char[strlen(r.name)+1];
strcpy(name,r.name);
}
ResumeA* ResumeA::Clone(){
return new ResumeA(*this);
}
void ResumeA::Set(const char *n)
{
if(strlen(n)<=strlen(name))
{
strcpy(name,n);
}
else
{
name = new char[strlen(n)+1];
strcpy(name,n);
}
}
void ResumeA::Show()
{
cout<<"ResumeA name:"<<name<<endl;
};
int main()
{
Resume *r1 = new ResumeA("A");
Resume *r2 = r1->Clone(); //A
Resume *r3 = r1->Clone(); //A
Resume *r4 = r1->Clone(); //A
delete r1; delete r2;
r1 = NULL; r2 = NULL;
r3->Show(); //A
r4->Show(); //A
r3->Set("ML");
r3->Show(); //ML
Resume *r5 = r3->Clone();
r4->Show(); //A
r5->Show(); //ML
delete r3; delete r4; delete r5;
r3 = NULL; r4 = NULL; r5 = NULL;
return 0;
}
原型模式要解决的问题就是不用重新初始化对象,而是动态地获取对象运行时的状态,你只需要对对象做部分属性或行为的修改即可。简化对象的创建,无需理会创建过程。
其中部分内容参考于:https://blog.csdn.net/wuzhekai1985/article/details/6667020