• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            emptysoul

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            命令模式(Command)的目標是將一個請求封裝成一個對象,因此可以參數(shù)化多個客戶的不同請求,將請求排除,記錄請求日志,并支持撤消操作。 結構圖如下:


            其實現(xiàn)思想是將一個請求封裝到一個類中(Command),再提供接收對象(Receiver),最后Command命令由Invoker調用。

            以一個電燈開關為例,命令的執(zhí)行、不執(zhí)行相對于開關的打開、關閉操作,由開關發(fā)出命令,電燈接收命令,結構圖如下:


            實現(xiàn)代碼:
            //Light.h
            class Light  
            {
            public:
                Light();
                
            virtual ~Light();

                
            void TurnOn();
                
            void TurnOff();
            };

            //Light.cpp
            #include "stdafx.h"
            #include 
            "Light.h"
            #include 
            <iostream>

            using namespace std;

            Light::Light()
            {

            }

            Light::
            ~Light()
            {

            }

            void Light::TurnOn()
            {
                cout 
            << "電燈打開了" << endl;
            }

            void Light::TurnOff()
            {
                cout 
            << "電燈關閉了" << endl;
            }

            //Command.h
            class Command  
            {
            public:
                
            virtual ~Command();

                
            virtual void Execute() = 0;
                
            virtual void UnExecute() = 0;
            protected:
                Command();
            };

            //Command.cpp
            #include "stdafx.h"
            #include 
            "Command.h"

            Command::Command()
            {
                
            }

            Command::
            ~Command()
            {

            }

            //LightCommand.h
            #include "Command.h"

            class Light;
            class LightCommand : public Command
            {
            public:
                LightCommand(Light
            *);
                
            virtual ~LightCommand();

                
            void Execute();
                
            void UnExecute();
            private:
                Light
            * m_pLight;
            };

            //LightCommand.cpp
            #include "stdafx.h"
            #include 
            "LightCommand.h"
            #include 
            "Light.h"

            LightCommand::LightCommand(Light
            * pLight)
            {
                m_pLight 
            = pLight;
            }

            LightCommand::
            ~LightCommand()
            {
                
            if(m_pLight != NULL)
                {
                    delete m_pLight;
                    m_pLight 
            = NULL;
                }
            }

            void LightCommand::Execute()
            {
                m_pLight
            ->TurnOn();
            }

            void LightCommand::UnExecute()
            {
                m_pLight
            ->TurnOff();
            }

            //Switch.h
            class Command;
            class Switch  
            {
            public:
                Switch(Command
            *);
                
            virtual ~Switch();

                
            void Open();
                
            void Close();
            private:
                Command
            * m_pCommand;
            };

            //Switch.cpp
            #include "stdafx.h"
            #include 
            "Switch.h"
            #include 
            "Command.h"

            Switch::Switch(Command
            * pCommand)
            {
                m_pCommand 
            = pCommand;
            }

            Switch::
            ~Switch()
            {

            }

            void Switch::Open()
            {
                m_pCommand
            ->Execute();
            }

            void Switch::Close()
            {
                m_pCommand
            ->UnExecute();
            }

            //main.cpp
            #include "stdafx.h"
            #include 
            "Switch.h"
            #include 
            "Light.h"
            #include 
            "LightCommand.h"

            int main(int argc, char* argv[])
            {
                Light
            * pLight = new Light;
                Command
            * pCommand = new LightCommand(pLight);
                Switch
            * pSwitch = new Switch(pCommand);
                pSwitch
            ->Open();
                pSwitch
            ->Close();
                
            return 0;
            }

            最后輸出為:
            電燈打開了
            電燈關閉了
            posted on 2009-02-14 20:13 emptysoul 閱讀(2136) 評論(2)  編輯 收藏 引用

            Feedback

            # re: 設計模式-命令模式 2012-01-31 16:26 live
            寫的好 支持一下  回復  更多評論
              

            # re: 設計模式-命令模式[未登錄] 2012-06-16 16:11 Li
            創(chuàng)建命令的時候,需要知道具體的實現(xiàn)者,如何實現(xiàn)者不存在,或者運行中不小心被釋放掉了,這個時候還怎樣用命令呢?  回復  更多評論
              

            一本一道久久精品综合| 久久久久久a亚洲欧洲aⅴ | 2021最新久久久视精品爱| 国产2021久久精品| 久久涩综合| 久久亚洲AV成人出白浆无码国产| 久久精品国产亚洲av麻豆色欲| 久久se精品一区二区| 久久综合九色欧美综合狠狠 | 99国产欧美精品久久久蜜芽 | 亚洲精品无码久久久久AV麻豆| 亚洲AV乱码久久精品蜜桃| 日本精品久久久久中文字幕8| 久久人人爽人人爽AV片| 国产精品禁18久久久夂久| 久久亚洲天堂| 亚洲一本综合久久| 日日噜噜夜夜狠狠久久丁香五月| 久久久WWW成人| 久久99精品国产一区二区三区| 性做久久久久久久久| 久久精品人人做人人爽电影| 性色欲网站人妻丰满中文久久不卡| 国产精品久久久久一区二区三区| 久久偷看各类wc女厕嘘嘘| 亚洲欧洲精品成人久久曰影片| 伊人色综合久久| 狠狠色噜噜狠狠狠狠狠色综合久久 | 一级做a爰片久久毛片毛片| 伊人久久免费视频| 久久精品国产秦先生| 国产精品九九九久久九九| 久久综合给久久狠狠97色| 久久婷婷色综合一区二区| 久久亚洲精品无码观看不卡| 国产福利电影一区二区三区久久老子无码午夜伦不 | 国产成人综合久久精品红| 久久精品亚洲精品国产欧美| 草草久久久无码国产专区| 精品午夜久久福利大片| 久久棈精品久久久久久噜噜|