简述:
原型模式用原型实例指定创建对象的种类,并且通过考北这些原型创建新的对象。
原型模式包括:抽象原型类、具体原型类。
抽象原型类:声明一个克隆自身的接扣。
具体原型类:继承自抽象原型类,实现一个克隆自身的操作。
原型模式代码:
1 #include <iostream> 2 using namespace std; 3 4 // 原型模式 5 // 抽象原型类 6 class CPrototype 7 { 8 private: 9 string m_szID; 10 11 public: 12 CPrototype() {} 13 14 CPrototype(string szID) 15 { 16 m_szID = szID; 17 } 18 19 void SetID(string szID) 20 { 21 m_szID = szID; 22 } 23 24 string GetID() 25 { 26 return m_szID; 27 } 28 29 virtual CPrototype& Clone() = 0; 30 }; 31 32 // 具体原型类 33 class CConcretePrototype : public CPrototype 34 { 35 public: 36 CConcretePrototype() {} 37 38 CConcretePrototype(string szID) : CPrototype(szID) {} 39 40 virtual CPrototype& Clone() 41 { 42 return (CPrototype&)*this; 43 } 44 }; 45 46 int main() 47 { 48 CConcretePrototype ConcretePrototype1("GHL"); 49 CConcretePrototype ConcretePrototype2 = (CConcretePrototype&)ConcretePrototype1.Clone(); 50 ConcretePrototype2.SetID("GGG"); 51 cout << ConcretePrototype1.GetID() << endl; 52 cout << ConcretePrototype2.GetID() << endl; 53 54 system("pause"); 55 return 0; 56 }
输出结果:
例:简历的克隆
注:本例在clone()函数中需要用到深拷贝,C#中可以继承ICloneable接口进行实现,但C++需要自己实现。
1 #include <iostream> 2 using namespace std; 3 4 // 工作经历 5 class CWorkExperience 6 { 7 private: 8 string m_szWorkDate; 9 string m_szCompany; 10 11 public: 12 void SetWorkDate(string szWorkDate) 13 { 14 m_szWorkDate = szWorkDate; 15 } 16 string GetWorkDate() 17 { 18 return m_szWorkDate; 19 } 20 21 void SetCompany(string szCompany) 22 { 23 m_szCompany = szCompany; 24 } 25 string GetCompany() 26 { 27 return m_szCompany; 28 } 29 30 CWorkExperience* Clone() 31 { 32 CWorkExperience* pWorkExperience = new CWorkExperience(); 33 pWorkExperience->SetWorkDate(GetWorkDate()); 34 pWorkExperience->SetCompany(GetCompany()); 35 return pWorkExperience; 36 } 37 }; 38 39 // 简历(具体原型类) 40 class CResume 41 { 42 private: 43 string m_szName; 44 string m_szSex; 45 string m_szAge; 46 CWorkExperience* m_pWorkExperience; 47 48 CResume(CWorkExperience* pWorkExperience) : m_pWorkExperience(NULL) 49 { 50 m_pWorkExperience = pWorkExperience->Clone(); 51 } 52 53 public: 54 CResume() : m_pWorkExperience(NULL) {} 55 56 CResume(string szName) : m_pWorkExperience(NULL) 57 { 58 m_szName = szName; 59 m_pWorkExperience = new CWorkExperience(); 60 } 61 62 ~CResume() 63 { 64 if (m_pWorkExperience) 65 { 66 delete m_pWorkExperience; 67 m_pWorkExperience = NULL; 68 } 69 } 70 71 void SetPersionalInfo(string szSex, string szAge) 72 { 73 m_szSex = szSex; 74 m_szAge = szAge; 75 } 76 77 void SetWorkExperience(string szWorkDate, string szCompany) 78 { 79 m_pWorkExperience->SetWorkDate(szWorkDate); 80 m_pWorkExperience->SetCompany(szCompany); 81 } 82 83 void Display() 84 { 85 cout << m_szName << " " << m_szSex << " " << m_szAge << endl; 86 cout << "工作经历: " << m_pWorkExperience->GetWorkDate() << " " 87 << m_pWorkExperience->GetCompany() << endl << endl; 88 } 89 90 CResume* Clone() 91 { 92 CResume* pResume = new CResume(m_pWorkExperience->Clone()); 93 pResume->m_szName = m_szName; 94 pResume->m_szSex = m_szSex; 95 pResume->m_szAge = m_szAge; 96 return pResume; 97 } 98 }; 99 100 int main() 101 { 102 CResume* pResume1 = new CResume("GHL"); 103 pResume1->SetPersionalInfo("男", "29"); 104 pResume1->SetWorkExperience("2018-2020", "MMM"); 105 106 CResume* pResume2 = pResume1->Clone(); 107 pResume2->SetWorkExperience("2016-2018", "BBB"); 108 109 pResume1->Display(); 110 pResume2->Display(); 111 112 if (pResume1) 113 { 114 delete pResume1; 115 pResume1 = NULL; 116 } 117 if (pResume2) 118 { 119 delete pResume2; 120 pResume2 = NULL; 121 } 122 123 system("pause"); 124 return 0; 125 }
输出结果: