// 从一个对象再创建另一个可定制的对象,而无需知道任何创建的细节。 // 并能提高创建的性能。说白了就是COPY技术,把一个对象完整的COPY出一份。 #include <iostream> using namespace std; class Prototype { public: Prototype(const string & str):id(str){} Prototype():id(""){} virtual Prototype* Clone() = 0; void setID(const string & ID) { id = ID; } void say() { cout << "我是" << id << endl; } private: string id; }; class ConcretePrototype1 : public Prototype { public: ConcretePrototype1(const string & str):Prototype(str){} ConcretePrototype1(){} Prototype* Clone() { ConcretePrototype1 *pt = new ConcretePrototype1(); *pt = *this; return pt; } }; class ConcretePrototype2 : public Prototype { public: ConcretePrototype2(const string & str):Prototype(str){} ConcretePrototype2(){} Prototype* Clone() { ConcretePrototype2 *pt = new ConcretePrototype2(); *pt = *this; return pt; } }; int main(void) { ConcretePrototype1 *a = new ConcretePrototype1("小王"); ConcretePrototype2 *b = (ConcretePrototype2*)a->Clone(); b->say(); a->setID("小李"); a->say(); b->say(); return 0; }