Prototype 模式也正是提供了自我复制的功能,就是说新对象的创建可以通过已有对象进行创建。Prototype 模式提供了一个通过已存在对象进行新对象创建的接口(Clone)
// Prototype
class Prototype{
public:
virtual ~Prototype();
virtual Prototype *Clone() const = 0;
protected:
Prototype();
};
//ConcretePrototype
class ConcretePrototype:public:Prototype{
public:
ConcretePrototype();
ConcretePrototype(const ConretePrototype &cp);
~ConcretePrototype();
Prototype *Clone() const{
return new ConcretePrototype(*this);
}
};
int main(void){
Prototype *p = new ConretePrototype();
Prototype *p1 = p->Clone();
return 0;
return 0;
}
“`