文章目錄
-
- 1、場景說明
- 2、代碼
-
- 2.1、結果
- 2.2、代碼
-
- 2.2.1 Waiter.h
- 2.2.2 Barbecuer.h
- 2.2.3 ICommand.h
- 2.2.4 BakeMuttonCmd.h
- 2.2.5 BakeChickenWingCmd.h
1、場景說明
相較基礎版:《大話設計模式》C++實作:23 指令模式(一)基礎版:
增加了時間、加訂單,删訂單(隻留了接口)、一次通知全部的菜單的功能。
《大話設計模式》C++實作:23 指令模式(二)進階版2 完善了删訂單功能~~
2、代碼
2.1、結果
main.cpp
//松耦合方式2: 進階版
#include "ICommand.h"
#include "BakeMuttonCmd.h"
#include "BakeChickenWingCmd.h"
#include "Waiter.h"
void test2()
{
CBarbecuer* boy = new CBarbecuer();
ICommand* pBakeMuttonCmd = new CBakeMuttonCmd(boy);
ICommand* pBakeChickenWingCmd = new CBakeChickenWingCmd(boy);
CWaiter* girl = new CWaiter();
//開門營業,點餐
girl->order(pBakeMuttonCmd);
girl->order(pBakeMuttonCmd);
girl->order(pBakeChickenWingCmd);
//girl->cancelOrder(pBakeMuttonCmd);//删除訂單還有點問題,先屏蔽
//通知
girl->notify();
//析構
delete boy;
delete pBakeMuttonCmd;
delete pBakeChickenWingCmd;
delete girl;
boy = nullptr;
pBakeMuttonCmd = nullptr;
pBakeChickenWingCmd = nullptr;
girl = nullptr;
}
int main()
{
test2();
system("pause");
return 0;
}
2.2、代碼
2.2.1 Waiter.h
#pragma once
#include "ICommand.h"
#include <List>
#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;
class CWaiter
{
public:
//點單
void order(ICommand* pCmd);
//取消點菜,還有點問題,後期維護
void cancelOrder(ICommand* pCmd);
//通知後廚
void notify();
private:
list<ICommand* > m_lCmds;
};
string cutTime()
{
struct tm t; // tm結構指針
time_t now; // 聲明time_t類型變量
time(&now); // 擷取系統日期和時間
localtime_s(&t, &now); // 擷取當地日期和時間
char szTmp[20] = "\0";
sprintf_s(szTmp, 20, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); // 産生"123"
string szResult(szTmp);
return szResult;
}
void CWaiter::order(ICommand* pCmd)
{
m_lCmds.push_back(pCmd);
cout << "增加訂單:" << pCmd->cmdName() << "\t時間:" << cutTime() << endl;
//to do: 如何及時得到庫存,當某一個菜沒有了,及時告知食客,修改訂單
}
void CWaiter::cancelOrder(ICommand* pCmd)
{
m_lCmds.remove(pCmd);
cout << "删除訂單:" << pCmd->cmdName() << "\t時間:" << cutTime() << endl;
}
//全部通知一遍
void CWaiter::notify()
{
for each (auto pCmd in m_lCmds)
{
pCmd->exec();
}
}
2.2.2 Barbecuer.h
(同《大話設計模式》C++實作:23 指令模式(一)基礎版的Barbecuer.h)
2.2.3 ICommand.h
#pragma once
#include "Barbecuer.h"
#include <string>
using namespace std;
class ICommand
{
public:
ICommand(CBarbecuer* pBarbecuer = nullptr);
~ICommand();
//執行指令(做菜)
virtual void exec() = 0;
//指令名稱(菜名)
virtual string cmdName() = 0;
protected:
//指令接受者(烤串人)
CBarbecuer* m_pReceiver{ nullptr };
};
ICommand::ICommand(CBarbecuer* pBarbecuer)
{
this->m_pReceiver = pBarbecuer;
}
ICommand::~ICommand()
{
m_pReceiver = nullptr;
}
2.2.4 BakeMuttonCmd.h
#pragma once
#include "ICommand.h"
class CBakeMuttonCmd :
public ICommand
{
public:
CBakeMuttonCmd(CBarbecuer* pBarbecuer = nullptr);
virtual void exec() override;
virtual string cmdName() override;
};
CBakeMuttonCmd::CBakeMuttonCmd(CBarbecuer* pBarbecuer /*= nullptr*/)
:ICommand(pBarbecuer)
{
}
void CBakeMuttonCmd::exec()
{
m_pReceiver->BakeMutton();//真正的妙處在這裡啊,執行時,指令讓烤串人去烤串
}
std::string CBakeMuttonCmd::cmdName()
{
return "烤羊肉串";
}
2.2.5 BakeChickenWingCmd.h
#pragma once
#include "ICommand.h"
class CBakeChickenWingCmd :
public ICommand
{
public:
CBakeChickenWingCmd(CBarbecuer* pBarbecuer = nullptr);
virtual void exec() override;
virtual string cmdName() override;
};
CBakeChickenWingCmd::CBakeChickenWingCmd(CBarbecuer* pBarbecuer /*= nullptr*/)
:ICommand(pBarbecuer)
{
}
void CBakeChickenWingCmd::exec()
{
m_pReceiver->BakeChickenWing();
}
std::string CBakeChickenWingCmd::cmdName()
{
return "烤雞翅";
}
此為《大話設計模式》學習心得系列 P240~~
指令模式相關連結:
《大話設計模式》C++實作:23 指令模式(一)基礎版
《大話設計模式》C++實作:23 指令模式(二)進階版2
《大話設計模式》C++實作:23 指令模式(三)抽象總結