标签:out turn type 模式 client ima inf justify alt
1) 意图:
用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象
2) 结构:
其中:
Prototype声明一个复制自身的接口
ConcretePrototype实现复制自身的操作
Client让一个原型复制自身从而创建一个新的对象
3) 适用性:
4) 举例:
1 #include <iostream> 2 class Prototype 3 { 4 public: 5 Prototype() {} 6 virtual ~Prototype() {} 7 virtual Prototype* Clone() = 0; 8 virtual void printCount() = 0; 9 }; 10 class ConcretePrototype : public Prototype 11 { 12 public: 13 ConcretePrototype() {} 14 ConcretePrototype(int num):m_count(num) {} 15 ConcretePrototype(const ConcretePrototype& p) 16 { 17 this->m_count = p.m_count; 18 } 19 20 virtual ~ConcretePrototype() {} 21 virtual Prototype* Clone() 22 { 23 return new ConcretePrototype(*this); 24 } 25 virtual void printCount() 26 { 27 std::cout << "m_count : " << m_count << std::endl; 28 } 29 private: 30 int m_count; 31 }; 32 33 int main() 34 { 35 Prototype* p = new ConcretePrototype(5); 36 Prototype* p1 = p->Clone(); 37 38 p->printCount(); 39 p1->printCount(); 40 41 delete p; 42 delete p1; 43 system("pause"); 44 }
标签:out turn type 模式 client ima inf justify alt
原文地址:https://www.cnblogs.com/ho966/p/12229664.html