當前位置: 湖州在線網首頁 >> 家居
設計模式19-Command(命令)模式-智能家居
發表時間:2020-08-31 10:05:51  閱讀:-   來源:互聯網
設計模式19-Command(命令)模式-智能家居

在軟件構建過程中,“行為請求者”與“行為實現者”通常呈現一種“緊耦合”。但在某些場合——比如需要對行為進行“記錄、撤銷/重做(undo/redo)、事務”等處理,這種無法抵御變化的緊耦合是不合適的。在這種情況下,如何將“行為請求者”與“行為實現者”解耦?

這種情況下就可以使用命令模式,命令模式即將一個請求(行為)封裝為一個對象,從而使你可用不同的請求對客戶端進行參數化;對請求排隊或記錄請求日志,以及支持可撤銷的操作。

UML圖:

設計模式19-Command(命令)模式-智能家居

舉個例子,當前智能家居很火熱,假設我們要設計一個手機app,可以通過按動上邊的控制按鈕控制臥室和廚房的燈,還能控制臥室中的音響的開關。app即是“動作的請求者”,而燈和音響就是“動作的執行者”。當我們按動app上的某個開關后,app就可以把相關的指令發送到我們的指定的家電上。這之中app和家電之間是解耦的,我們完全可以通過設置,添加、修改或刪除其它的家電控制功能,而不需要修改app的代碼。

設計模式19-Command(命令)模式-智能家居

代碼如下:

#include <iostream>
#include <vector>using namespace std;
class Command
{public:
virtual void execute() = 0;
};class NoCommand : public Command
{public:
void execute() {};
};class Light
{public:
Light(string location) {
m_sLocation = location; } void on() {
printf("%s light is on\n", m_sLocation.c_str());
} void off() {
printf("%s light is off\n", m_sLocation.c_str());
}private:
string m_sLocation;
};class LightOffCommand : public Command
{public:
LightOffCommand(string location) :m_Light(location) {}
void execute() {
m_Light.off(); }private:
Light m_Light;};class LightOnCommand : public Command
{public:
LightOnCommand(string location) :m_Light(location) {}
void execute() {
m_Light.on(); }private:
Light m_Light;};class Stereo
{public:
Stereo(string location) {
m_sLocation = location; } void on() {
printf("%s stereo is on\n", m_sLocation.c_str());
} void off() {
printf("%s stereo is off\n", m_sLocation.c_str());
} void setCD() {
printf("%s stereo is set for CD input\n", m_sLocation.c_str());
} void setDVD() {
printf("%s stereo is set for DVD input\n", m_sLocation.c_str());
} void setRadio() {
printf("%s stereo is set for Radio\n", m_sLocation.c_str());
} void setVolume(int volume) {
printf("%s Stereo volume set to %d\n", m_sLocation.c_str(), volume);
}private:
string m_sLocation;
};class StereoOnWithCDCommand : public Command
{public:
StereoOnWithCDCommand(string location) :m_Stereo(location) {}
void execute() {
m_Stereo.on(); m_Stereo.setCD(); m_Stereo.setVolume(11);
}private:
Stereo m_Stereo;};class StereoOffCommand : public Command
{public:
StereoOffCommand(string location) :m_Stereo(location) {}
void execute() {
m_Stereo.off(); }private:
Stereo m_Stereo;};class RemoteControl
{public:
RemoteControl() { for (int i = 0; i < 7; i++)
{ Command* noCommandOn = new NoCommand();
m_OnCommands.push_back(noCommandOn); Command* noCommandOff = new NoCommand();
m_OffCommands.push_back(noCommandOff); } } ~RemoteControl() { for (int i = 0; i < 7; i++)
{ delete m_OnCommands.at(i);
delete m_OffCommands.at(i);
} m_OnCommands.clear(); m_OffCommands.clear(); } void setCommand(int slot, Command* pOnCommand, Command* pOffCommand) {
delete m_OnCommands.at(slot);
m_OnCommands.at(slot) = pOnCommand; delete m_OffCommands.at(slot);
m_OffCommands.at(slot) = pOffCommand; } void onButtonWasPushed(int slot) {
m_OnCommands.at(slot)->execute(); } void offButtonWasPushed(int slot) {
m_OffCommands.at(slot)->execute(); }private:
vector<Command*> m_OnCommands; vector<Command*> m_OffCommands;};int main(){ RemoteControl remoteControl; LightOffCommand* pLivingRoomLightOff = new LightOffCommand("臥室");
LightOffCommand* pKitchenLightOff = new LightOffCommand("Kitchen");
LightOnCommand* pLivingRoomLightOn = new LightOnCommand("Living Room");
LightOnCommand* pKitchenLightOn = new LightOnCommand("Kitchen");
StereoOnWithCDCommand* pStereoOnWithCD = new StereoOnWithCDCommand("Living Room");
StereoOffCommand* pStereoOff = new StereoOffCommand("Living Room");
remoteControl.setCommand(0, pLivingRoomLightOn, pLivingRoomLightOff);
remoteControl.setCommand(1, pKitchenLightOn, pKitchenLightOff);
remoteControl.setCommand(2, pStereoOnWithCD, pStereoOff);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(2);
return 0;
}

運行結果:

Living Room light is on
Living Room light is off
Kitchen light is on
Kitchen light is off
Living Room stereo is on
Living Room stereo is set for CD input
Living Room Stereo volume set to 11
Living Room stereo is off
設計模式19-Command(命令)模式-智能家居


推薦閱讀:齊魯都市網

圖片推薦
關于我們 | 聯系我們 | XML地圖 | 網站地圖TXT | 版權聲明

Copyright 2020 湖州在線網 版權所有 本網拒絕一切非法行為 歡迎監督舉報 如有錯誤信息 歡迎糾正


bg视讯就没有赢过