• <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)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

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


            其實(shí)現(xiàn)思想是將一個(gè)請(qǐng)求封裝到一個(gè)類中(Command),再提供接收對(duì)象(Receiver),最后Command命令由Invoker調(diào)用。

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


            實(shí)現(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 
            << "電燈關(guān)閉了" << 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;
            }

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

            Feedback

            # re: 設(shè)計(jì)模式-命令模式 2012-01-31 16:26 live
            寫的好 支持一下  回復(fù)  更多評(píng)論
              

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


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产成人精品久久亚洲高清不卡 | 亚洲国产精品无码久久久秋霞2| 99久久国产综合精品麻豆| 久久精品国产亚洲AV香蕉| 狠狠综合久久综合中文88| 思思久久99热只有频精品66| 久久久精品人妻一区二区三区四| 91性高湖久久久久| 一级做a爰片久久毛片看看| 欧美午夜精品久久久久免费视| 2020最新久久久视精品爱| 亚洲熟妇无码另类久久久| 久久99毛片免费观看不卡| 久久精品中文字幕大胸| 秋霞久久国产精品电影院| 中文字幕热久久久久久久| 九九久久精品无码专区| 久久久国产精品亚洲一区| 欧美久久综合九色综合| 91精品国产高清久久久久久国产嫩草| 大香伊人久久精品一区二区| 久久国产美女免费观看精品| 久久99精品国产99久久6男男| 亚洲日本va中文字幕久久| 久久久久九九精品影院| 国产91久久综合| 91久久精品国产免费直播| 国产精品久久精品| 久久精品国产亚洲av影院| 无码AV波多野结衣久久| 亚洲精品乱码久久久久久中文字幕| 性高湖久久久久久久久AAAAA | 亚洲?V乱码久久精品蜜桃| 欧美精品一区二区精品久久| 国产精品久久久久影院色| 久久久久久久人妻无码中文字幕爆 | 精品无码久久久久久久动漫| 99久久精品免费看国产| 久久国产成人精品麻豆| 亚洲国产成人久久综合碰碰动漫3d| 久久久久久人妻无码|