• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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


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

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


            實現代碼:
            //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 閱讀(2125) 評論(2)  編輯 收藏 引用

            Feedback

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

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

            久久久久99这里有精品10| 久久精品蜜芽亚洲国产AV| 久久久中文字幕日本| 香蕉99久久国产综合精品宅男自 | 91久久精一区二区三区大全| 久久久久久综合一区中文字幕| 91超碰碰碰碰久久久久久综合| 久久久久亚洲精品中文字幕| 久久久久久久女国产乱让韩| 72种姿势欧美久久久久大黄蕉| 日本高清无卡码一区二区久久 | 97视频久久久| 国产精品99久久久久久人| 中文字幕久久欲求不满| 无码八A片人妻少妇久久| 中文字幕久久欲求不满| 亚洲精品乱码久久久久久蜜桃图片| 99热热久久这里只有精品68| 久久无码AV中文出轨人妻| 国产成人久久777777| 俺来也俺去啦久久综合网| 久久婷婷是五月综合色狠狠| 99久久无码一区人妻| 无码国产69精品久久久久网站| 日本精品久久久久影院日本| 一本伊大人香蕉久久网手机| 久久久国产乱子伦精品作者| 亚洲色欲久久久综合网| 久久中文字幕人妻熟av女| 四虎久久影院| 午夜福利91久久福利| 国产午夜精品久久久久九九| 91精品国产高清久久久久久io| 性高湖久久久久久久久| 亚洲精品乱码久久久久久蜜桃图片 | 久久国产精品一国产精品金尊 | 色8激情欧美成人久久综合电| 99热成人精品免费久久| 精品久久久久久99人妻| 久久精品国产精品亜洲毛片| 久久国产乱子伦精品免费午夜|