用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
#include <iostream>
using namespace std;
class Prototype
{
public:
virtual ~Prototype(){}
virtual Prototype* Clone() const = 0;
protected:
Prototype(){}
};
class ConcretePrototype:public Prototype
{
public:
int i;
ConcretePrototype(){}
ConcretePrototype(const ConcretePrototype& cp)
{
cout <<"ConcretePrototype copy..." << endl;
}
Prototype* Clone()const
{
return new ConcretePrototype(*this) ;
}
~ConcretePrototype(){}
};
int main()
{
Prototype *p = new ConcretePrototype() ;
Prototype *p1 = p->Clone();
return 0;
}