原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创建的细节 下面是其UML模型和代码实现: //prototype.h #ifndef AFX_CLASS_PROTOTYPE #define AFX_CLASS_PROTOTYPE class Prototype { public: Prototype( string id ) { this->id = id; } virtual Prototype *Clone() = 0; public: string id; }; #endif #ifndef AFX_CLASS_CONCRETEPROTOTYPE #define AFX_CLASS_CONCRETEPROTOTYPE class ConcretePrototype1 : public Prototype { public: ConcretePrototype1( string id ) : Prototype( id ) { this->id = id; } virtual Prototype *Clone() { ConcretePrototype1 *p = new ConcretePrototype1( id ); *p = *this; return p; } public: string id; }; #endif #include <iostream> #include <string> #include <conio.h> using namespace std; #include "prototype.h" int main( int argc , char *argv[] ) { ConcretePrototype1 *p1 = new ConcretePrototype1( " 1 " ); ConcretePrototype1 *c1 = (ConcretePrototype1 *)p1->Clone(); cout<< " clone : " << c1->id ; getch(); return 1; }