|
namespace DesignPattern_Prototype { // class Prototype class Prototype //抽象基类 { public: virtual Prototype* Clone() = 0 ; } ;
// class ConcretePrototype1 class ConcretePrototype1 : public Prototype { public: virtual Prototype* Clone() { ConcretePrototype1 *p = new ConcretePrototype1() ; *p = *this ; //复制对象 return p ; } } ;
// class ConcretePrototype2 class ConcretePrototype2 : public Prototype { public: virtual Prototype* Clone() { ConcretePrototype2 *p = new ConcretePrototype2() ; *p = *this ; //复制对象 return p ; } } ; }
客户端代码: { using namespace DesignPattern_Prototype ;
ConcretePrototype1 *obj1 = new ConcretePrototype1() ;//原型对象1 ConcretePrototype2 *obj2 = new ConcretePrototype2() ;//原型对象2
Prototype *newobj1 = obj1->Clone() ;//克隆对象1 Prototype *newobj2 = obj2->Clone() ;//克隆对象2
//使用复制出的对象newobj1和newobj2 } |