原型模式(Prototype Pattern):
使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。原型模式是一种对象创建型模式
应用实例
描述如下:
未使用原型模式
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template<class T>
string ConvertToString(T value) {
stringstream ss;
ss << value;
return ss.str();
}
class OrderApi {
public:
virtual int getOrderNum() { return m_orderProductNum; };
virtual void setOrderNum(int num) { m_orderProductNum = num; };
virtual string getOrderContent() = 0;
protected:
OrderApi() {};
int m_orderProductNum;
};
class HomeOrder : public OrderApi {
public:
virtual string getOrderContent() {
return "客户是:" + m_customerName + " 订单的数量:" + ConvertToString<int>(m_orderProductNum) + " 订单id:" + m_productId;
}
void setCustomName(string name) {
m_customerName = name;
}
string getCustomName() { return m_customerName; };
void setId(string id) {
m_productId = id;
}
string getId() { return m_productId; };
private:
string m_customerName;
string m_productId;
};
class AboardOrder : public OrderApi {
public:
virtual string getOrderContent() {
return "客户是:" + m_customerName + " 订单的数量:" + ConvertToString<int>(m_orderProductNum) + " 订单id:" + m_productId;
}
void setCustomName(string name) {
m_customerName = name;
}
string getCustomName() { return m_customerName; };
void setId(string id) {
m_productId = id;
}
string getId() { return m_productId; };
private:
string m_customerName;
string m_productId;
};
class OrderBusiness {
public:
void saveOrder(OrderApi* order);
};
void OrderBusiness::saveOrder(OrderApi* order) {
while (order->getOrderNum() > 200)
{
OrderApi* neworder = nullptr;
if (dynamic_cast<HomeOrder*>(order)!= nullptr)
{
HomeOrder* p2 = new HomeOrder;
HomeOrder* p1 = static_cast<HomeOrder*>(order);
p2->setOrderNum(200);
p2->setCustomName(p1->getCustomName());
p2->setId(p1->getId());
neworder = p2;
}
if (dynamic_cast<AboardOrder*>(order) != nullptr)
{
HomeOrder* p2 = new HomeOrder;
HomeOrder* p1 = static_cast<HomeOrder*>(order);
p2->setOrderNum(200);
p2->setCustomName(p1->getCustomName());
p2->setId(p1->getId());
neworder = p2;
}
//原来的订单更新数量
order->setOrderNum(order->getOrderNum() - 200);
cout << "New Order:" << neworder->getOrderContent() << endl;
}
cout << "Order:" << order->getOrderContent() << endl;
}
int main(void) {
//system("killtask /m prototype.exe");
HomeOrder* home = new HomeOrder();
home->setOrderNum(512);
home->setCustomName("zj");
home->setId("c++");
OrderBusiness* obj = new OrderBusiness;
obj->saveOrder(home);
return 0;
}
使用原型模式
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template<class T>
string ConvertToString(T value) {
stringstream ss;
ss << value;
return ss.str();
}
class OrderApi {
public:
virtual int getOrderNum() { return m_orderProductNum; };
virtual void setOrderNum(int num) { m_orderProductNum = num; };
virtual string getOrderContent() = 0;
virtual OrderApi* cloneOrder() = 0;
protected:
OrderApi() {};
int m_orderProductNum;
};
class HomeOrder : public OrderApi {
public:
virtual string getOrderContent() {
return "客户是:" + m_customerName + " 订单的数量:" + ConvertToString<int>(m_orderProductNum) + " 订单id:" + m_productId;
}
void setCustomName(string name) {
m_customerName = name;
}
string getCustomName() { return m_customerName; };
void setId(string id) {
m_productId = id;
}
string getId() { return m_productId; };
virtual OrderApi* cloneOrder() {
HomeOrder* p2 = new HomeOrder;
p2->setOrderNum(200);
p2->setCustomName(m_customerName);
p2->setId(m_productId);
return p2;
}
private:
string m_customerName;
string m_productId;
};
class AboardOrder : public OrderApi {
public:
virtual string getOrderContent() {
return "客户是:" + m_customerName + " 订单的数量:" + ConvertToString<int>(m_orderProductNum) + " 订单id:" + m_productId;
}
void setCustomName(string name) {
m_customerName = name;
}
string getCustomName() { return m_customerName; };
void setId(string id) {
m_productId = id;
}
string getId() { return m_productId; };
private:
string m_customerName;
string m_productId;
};
class OrderBusiness {
public:
void saveOrder(OrderApi* order);
};
void OrderBusiness::saveOrder(OrderApi* order) {
while (order->getOrderNum() > 200)
{
OrderApi* neworder = nullptr;
if (dynamic_cast<HomeOrder*>(order) != nullptr)
{
HomeOrder* p2 = new HomeOrder;
HomeOrder* p1 = static_cast<HomeOrder*>(order);
p2->setOrderNum(200);
p2->setCustomName(p1->getCustomName());
p2->setId(p1->getId());
neworder = p2;
}
if (dynamic_cast<AboardOrder*>(order) != nullptr)
{
HomeOrder* p2 = new HomeOrder;
HomeOrder* p1 = static_cast<HomeOrder*>(order);
p2->setOrderNum(200);
p2->setCustomName(p1->getCustomName());
p2->setId(p1->getId());
neworder = p2;
}
//原来的订单更新数量
order->setOrderNum(order->getOrderNum() - 200);
cout << "New Order:" << neworder->getOrderContent() << endl;
}
cout << "Order:" << order->getOrderContent() << endl;
}
int main(void) {
//system("KILLTASK /M Prototype.exe");
HomeOrder* home = new HomeOrder();
home->setOrderNum(512);
home->setCustomName("ZJ");
home->setId("C++");
OrderBusiness* obj = new OrderBusiness;
obj->saveOrder(home);
return 0;
}