本文以 买卖双方 交易为例子,根据编程模式做的实例
OOP面向对象编程
class BusinessManInterFace
{
public:
BusinessManInterFace() {};
virtual ~BusinessManInterFace() = 0;
public:
//virtual void PrintAccountBook() = 0;
// 是否可以交易
virtual bool CheckBusiness(BusinessManInterFace* man) = 0;
// 开始交易
virtual void Start() = 0;
virtual int DoBusiness(int count) = 0;
public:
std::string m_name;
std::string m_goods;
int m_realCount;
double m_price;
};
class ManSell;
class ManBuy;
class ManBuy:public BusinessManInterFace
{
public:
ManBuy() {}
virtual ~ManBuy() {}
public:
bool CheckBusiness(ManSell* seller)
{
if (seller->m_price <= (m_price + 2) && seller->m_realCount >0)
{
if (seller->CheckBusiness(this))
{
m_okSellers.push_back(seller);
}
return true;
}
}
virtual void Start()
{
for (auto seller : m_okSellers)
{
Do(seller);
}
}
virtual int DoBusiness(int count)
{
}
virtual bool Do(ManSell* seller)
{
if (m_realCount > m_curCount)
{
int count = seller->DoBusiness(m_realCount - m_curCount);
if (count > 0)
{
int total = m_curCount + count;
m_bOK = total == m_realCount ? true: false;
if (total <= m_realCount)
{
m_curCount = total;
return true;
}
}
}
return false;
}
private:
std::vector<ManSell*> m_okSellers;
bool m_bOK = false;
int m_curCount;
};
class ManSell :public BusinessManInterFace
{
public:
ManSell() {}
virtual ~ManSell() {}
public:
bool CheckBusiness(ManBuy* buyer)
{
if (buyer->m_price >= (m_price - 1))
{
m_okBueyers.push_back(buyer);
return true;
}
return false;
}
virtual void Start()
{
// 被动的
}
virtual int DoBusiness(int count)
{
if (count < m_realCount)
{
m_realCount = m_realCount - count;
return count;
}
else
{
return m_realCount;
}
}
private:
std::vector<ManBuy*> m_okBueyers;
};
class BusinessPlat
{
public:
void RegistBuyer(ManBuy* buyer)
{
m_buyers.push_back(buyer);
}
void RegistSeller(ManSell* buyer)
{
m_sellers.push_back(buyer);
}
public:
void StartBusiness()
{
for (auto buy : m_buyers)
{
for (auto sell : m_sellers)
{
buy->CheckBusiness(sell);
}
}
// 先买方市场
for (auto buy : m_buyers)
{
buy->Start();
}
// 再卖方市场
for (auto sell : m_sellers)
{
sell->Start();
}
}
private:
std::vector<ManBuy* > m_buyers;
std::vector<ManSell* > m_sellers;
};
// OOP模式
void TestOOP()
{
BusinessPlat* plat = new BusinessPlat();
ManBuy *buyer = new ManBuy();
ManSell* seller = new ManSell();
plat->RegistBuyer(buyer);
plat->RegistSeller(seller);
plat->StartBusiness();
}
数据流编程
// 数据流
struct BusinessData
{
std::string goods;
bool bBuyer;
int realCount;
int curCount;
double price;
double priceOver; // 浮动价值
};
class BusinessDataPlat
{
public:
void RegistBuyer(BusinessData& buyer)
{
m_buyers.push_back(buyer);
}
void RegistSeller(BusinessData& buyer)
{
m_sellers.push_back(buyer);
}
// 核心逻辑在此,数据流 模式,集中业务逻辑在一处
static int DoBusiness(BusinessData& buy, BusinessData& sell)
{
int count = buy.realCount - buy.curCount;
if (count <= 0)
return count;
if (buy.priceOver > sell.priceOver)
{
int count = sell.realCount - count;
if (count > 0)
{
buy.curCount += count;
sell.realCount -= count;
return count;
}
}
return -1;
}
public:
void StartBusiness()
{
for (auto buy : m_buyers)
{
for (auto sell : m_sellers)
{
DoBusiness(buy,sell);
}
}
}
private:
std::vector<BusinessData> m_buyers;
std::vector<BusinessData> m_sellers;
};
void TestDataFlow()
{
BusinessData buyer = { "",true,10,0,100 };
BusinessData seller = { "",false,10,0,100 };
BusinessDataPlat* plat = new BusinessDataPlat();
plat->RegistBuyer(buyer);
plat->RegistSeller(seller);
plat->StartBusiness();
}
过程式编程
// 过程
void TestMethodFlow()
{
BusinessData buyer = { "",true,10,0,100 };
BusinessData seller = { "",false,10,0,100 };
// 直接调用过程
BusinessDataPlat::DoBusiness(buyer, seller);
}
解析式编程
// 数据流解析器式
void TestProgram()
{
QString buyData = R"(
{
"bBuyer":true,
"goods":"",
"price":100,
"curCount":0,
"realCount":0,
}
)";
QJsonObject buyer = QJsonDocument::fromJson(buyData.toUtf8()).object();
QString sellData = R"(
{
"bBuyer":false,
"goods":"",
"price":100,
"curCount":100,
"realCount":100,
}
)";
QJsonObject seller = QJsonDocument::fromJson(buyData.toUtf8()).object();
BusinessDataPlat::DoBusinessByData(buyer, seller);
}