原型模式
意图
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
类图
代码
#include <iostream>
#include <string>
using namespace std;
class Prototype
{
private:
string id;
public:
Prototype(string id)
{
this->id = id;
}
virtual Prototype *Clone()=0;
void SetId(string id)
{
this->id = id;
}
void Show()
{
cout<<this->id<<endl;
}
};
class ConcreteProtatype1: Prototype
{
public:
ConcreteProtatype1(string id):Prototype(id)
{
}
Prototype *Clone()
{
return this;
}
};
int main()
{
ConcreteProtatype1 *pConPro1 = new ConcreteProtatype1("wode");
Prototype *pConPro2 = pConPro1->Clone();
//不注释掉就会出错,要研究下为啥出错
// pConPro1->SetId("nide");
// pConPro2->Show();
}
附件列表