原型模式(prototype)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 #include <sstream> 4 #include <typeinfo> 5 using namespace std; 6 template <class T> 7 string iToStr(T value) { 8 stringstream ss; 9 ss << value; 10 return ss.str(); 11 } 12 class OrderApi { 13 public: 14 virtual int getOrderProductNum() = 0; 15 virtual void setOrderProductNum(int num) = 0; 16 virtual string getOrderContent() = 0; 17 virtual OrderApi* cloneOrder() = 0; 18 protected: 19 OrderApi() {} 20 }; 21 22 class HomeOrder : public OrderApi { 23 public: 24 int getOrderProductNum() { 25 return m_orderProductNum; 26 } 27 void setOrderProductNum(int num) { 28 m_orderProductNum = num; 29 } 30 string getOrderContent() { 31 return "本次订单的客户是" + m_strCustomerName + "订单的id" + m_strProductId + "订单的数量是" + iToStr(m_orderProductNum); 32 } 33 34 void setCustomerName(string strCustomerName) { 35 m_strCustomerName = strCustomerName; 36 } 37 38 string getCustomerName() { 39 return m_strCustomerName; 40 } 41 42 void setProductId(string strProductId) { 43 m_strProductId = strProductId; 44 } 45 string getProductId() { 46 return m_strProductId; 47 } 48 49 //重构 50 OrderApi* cloneOrder(); 51 private: 52 string m_strCustomerName; 53 string m_strProductId; 54 int m_orderProductNum; 55 }; 56 57 OrderApi* HomeOrder::cloneOrder() { 58 59 HomeOrder *pHomeOrder = new HomeOrder; 60 pHomeOrder->setCustomerName(m_strCustomerName); 61 pHomeOrder->setProductId(m_strProductId); 62 pHomeOrder->setOrderProductNum(m_orderProductNum); 63 return pHomeOrder; 64 } 65 66 67 class AboardOrder : public OrderApi { 68 public: 69 int getOrderProductNum() { 70 return m_orderProductNum; 71 } 72 void setOrderProductNum(int num) { 73 m_orderProductNum = num; 74 } 75 string getOrderContent() { 76 return "本次订单的客户是" + m_strCustomerName + "订单的id" + m_strProductId + "订单的数量是" + iToStr(m_orderProductNum); 77 } 78 79 void setCustomerName(string strCustomerName) { 80 m_strCustomerName = strCustomerName; 81 } 82 83 string getCustomerName() { 84 return m_strCustomerName; 85 } 86 87 void setProductId(string strProductId) { 88 m_strProductId = strProductId; 89 } 90 string getProductId() { 91 return m_strProductId; 92 } 93 //重构 94 OrderApi* cloneOrder(); 95 private: 96 string m_strCustomerName; 97 string m_strProductId; 98 int m_orderProductNum; 99 }; 100 101 OrderApi* AboardOrder::cloneOrder() { 102 103 AboardOrder *pHomeOrder = new AboardOrder; 104 pHomeOrder->setCustomerName(m_strCustomerName); 105 pHomeOrder->setProductId(m_strProductId); 106 pHomeOrder->setOrderProductNum(m_orderProductNum); 107 return pHomeOrder; 108 } 109 110 class OrderBusiness { 111 public: 112 void saveOrder(OrderApi* pOrder); 113 }; 114 115 void OrderBusiness::saveOrder(OrderApi* pOrder) { 116 //判读一下,工件的数量有无超过200 117 while (pOrder->getOrderProductNum() > 200) { 118 //新建一个订单 119 OrderApi* pNewOrder = pOrder->cloneOrder(); 120 pNewOrder->setOrderProductNum(200); 121 122 pOrder->setOrderProductNum(pOrder->getOrderProductNum() - 200); 123 cout << "新订单是" << pNewOrder->getOrderContent() << endl; 124 } 125 //不超过200个 126 cout << "最终的订单是" << pOrder->getOrderContent() << endl; 127 } 128 129 int main(void) { 130 HomeOrder* pHome = new HomeOrder; 131 pHome->setOrderProductNum(512); 132 pHome->setCustomerName("xcj&dst"); 133 pHome->setProductId("C++设计模式精讲"); 134 OrderBusiness* pOb = new OrderBusiness(); 135 pOb->saveOrder(pHome); 136 system("pause"); 137 return 0; 138 }