23种设计模式C++源码与UML实现--命令模式
命令模式
Command模式也叫命令模式,是行為設計模式的一種。Command模式通過被稱為Command的類封裝了對目標對象的調用行為以及調用參數。
在面向對象的程序設計中,一個對象調用另外一個對象,一邊情況下調用的過程是,創(chuàng)建目標對象實例;設置調用參數,調用目標對象的方法。
但是有些情況下有必要使用一個專門的類對這種調用過程加以封裝,我們把這種專門的類稱作Command類。
整個調用過程比較繁雜,或者存在多處這種調用。這時,使用Command類對該調用加以封裝,便于功能的再利用。
調用前后需要對調用參數進行某些處理。調用前后需要進行某些額外的處理,比如日志,緩存、記錄歷史操作等。
角色和職責
Command
Command命令的抽象類
ConcreteCommand
ConcreteCommand的具體實現類
Receiver
Invorker
通過Invorker執(zhí)行Command對象
適用于
是將一個請求封裝為一個對象,從而使你可以使用不同的請求,對客戶端進行參數初始化;對請求排隊或記錄請求日志,以及支持可撤銷的操作
僅僅實現命令的執(zhí)行
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-GeX5EdLe-1606658989604)(image/image-20201123232731749.png)]
首先實現,后半部分,也就是實現命令的執(zhí)行,前面半部分相當于當看病的人多的時候需要,需要排隊,這似乎invoker就開始登場啦。
// // Created by andrew on 2020/11/23. // #include <iostream>using namespace std;class Doctor { public:void treatEye() {cout << "treat eye" << endl;}void treatNose() {cout << "treat nose" << endl;} };class CommandTreatEye { public:explicit CommandTreatEye(Doctor *doctor) {m_doctor = doctor;}void treat() {m_doctor->treatEye();}private:Doctor *m_doctor; };class CommandTreatNose { public:explicit CommandTreatNose(Doctor *doctor) {m_doctor = doctor;}void treat() {m_doctor->treatNose();}private:Doctor *m_doctor; };int main(int argc, char *argv[]) {// 通過一個命令 調用醫(yī)生實現病的治療Doctor *dcotor = new Doctor;CommandTreatEye *commandTreatEye = new CommandTreatEye(dcotor);commandTreatEye->treat();delete commandTreatEye;delete dcotor;return 0; }完整的實現,支持排隊+執(zhí)行的命令模式
// // Created by andrew on 2020/11/23. // #include <iostream> #include <list>using namespace std;class Doctor { public:void treatEye() {cout << "treat eye" << endl;}void treatNose() {cout << "treat nose" << endl;} };class Command { public:virtual void treat() = 0;virtual ~Command() = default; };class CommandTreatEye : public Command { public:explicit CommandTreatEye(Doctor *doctor) {m_doctor = doctor;}void treat() override {m_doctor->treatEye();}private:Doctor *m_doctor; };class CommandTreatNose : public Command { public:explicit CommandTreatNose(Doctor *doctor) {m_doctor = doctor;}void treat() override {m_doctor->treatNose();}private:Doctor *m_doctor; };//小護士 class BeautyNurse { public:explicit BeautyNurse(Command *command) {this->command = command;}void SubmittedCase() { // 提交病歷 下單命令command->treat();}private:Command *command; };class HeadNurse { public:HeadNurse() {m_list.clear();}void setCommand(Command *command) {m_list.push_back(command);}void SubmittedCase() { // 提交命令for (auto & it : m_list) {it->treat();}}private:list<Command *> m_list; };void nurseCommand() {BeautyNurse *beautyNurse = nullptr;Doctor *doctor = nullptr;Command *command = nullptr;doctor = new Doctor; //command = new CommandTreatNose(doctor);beautyNurse = new BeautyNurse(command);beautyNurse->SubmittedCase();delete doctor;delete command;delete beautyNurse; }void headNurseCommand() { // 護士長 提交病歷 給以上看病HeadNurse *headNurse = NULL;Doctor * doctor = NULL;Command *command1 = NULL;Command *command2 = NULL;doctor = new Doctor;command1 = new CommandTreatEye(doctor);command2 = new CommandTreatNose(doctor);headNurse = new HeadNurse;headNurse->setCommand(command1);headNurse->setCommand(command2);headNurse->SubmittedCase(); // 護士長 批量下單命令delete doctor;delete command1;delete command2;delete headNurse; }int main(int argc, char *argv[]) {// 通過一個命令 調用醫(yī)生實現病的治療 命令的執(zhí)行部分實現/* Doctor *dcotor = new Doctor;CommandTreatEye *commandTreatEye = new CommandTreatEye(dcotor);commandTreatEye->treat();delete commandTreatEye;delete dcotor;*///nurseCommand();headNurseCommand();return 0; }總結
以上是生活随笔為你收集整理的23种设计模式C++源码与UML实现--命令模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【2016年第1期】专题导读:农业大数据
- 下一篇: 作者:刘玮(1977-),男,中国科学院