prototype 模式结构图:
实现:
1 #ifndef _PROTOTYPE_H_ 2 #define _PROTOTYPE_H_ 3 4 class Prototype 5 { 6 public: 7 virtual ~Prototype(); 8 virtual Prototype* Clone() const = 0; 9 10 protected: 11 Prototype(); 12 13 private: 14 }; 15 16 class ConcretePrototype:public Prototype 17 { 18 public: 19 ConcretePrototype(); 20 ConcretePrototype(const ConcretePrototype& cp); 21 ~ConcretePrototype(); 22 Prototype* Clone()const; 23 protected: 24 private: 25 }; 26 27 #endif
1 #include "Prototype.h" 2 #include <iostream> 3 using namespace std; 4 5 Prototype::Prototype() 6 { 7 8 } 9 Prototype::~Prototype() 10 { 11 12 } 13 Prototype* Prototype::Clone()const 14 { 15 return 0; 16 } 17 ConcretePrototype::ConcretePrototype() 18 { 19 20 }; 21 ConcretePrototype::~ConcretePrototype() 22 { 23 24 } 25 ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp) 26 { 27 cout<<"ConcretePrototype copy..."<<endl; 28 } 29 Prototype* ConcretePrototype::Clone()const 30 { 31 return new ConcretePrototype(*this); 32 }