• <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>
            蝸牛的家
            男兒當自強
            posts - 48,  comments - 21,  trackbacks - 0
            意圖:
            動態的給一個對象添加一些額外的職責。比生成子類更為靈活
            UML結構圖:

            適用:
            在不影響其他對象的情況下,以動態,透明的方式給單個對象添加職責
            處理那些不可撤消的職責
            當不能采用生成子類的方式進行擴充時
            //test.h
            //////////////////////////////////////////////////////////////////////////
            class Component
            {
            public:
                Component()
            {}
                
            virtual ~Component(){}

                
            //純虛函數
                virtual void Operation() = 0;
            }
            ;

            //抽象基類,維護一個指向Component對象的指針
            class Decorator : public Component
            {
            public:
                Decorator(Component
            * pComponent) : m_pComponent(pComponent){}
                
            virtual ~Decorator();
            protected:
                Component
            * m_pComponent;
            }
            ;

            //派生自Component,需要給他動態添加職責
            class ConCreateComponent : public Component
            {
            public:
                ConCreateComponent()
            {}
                
            virtual ~ConCreateComponent(){}

                
            virtual void Operation();
            }
            ;

            //派生自Decorator,為ConCreateComponent動態添加職責
            class ConCreateDecorator : public Decorator
            {
            public:
                ConCreateDecorator(Component
            * pComponent) : Decorator(pComponent){}
                
            virtual ~ConCreateDecorator(){}

                
            virtual void Operation();
            private:
                
            void AddedBehavior(); //動態添加的職責
            }
            ;

            // test.cpp : Defines the entry point for the console application.
            //

            #include "stdafx.h"
            #include 
            <iostream>
            #include "test.h"

            //////////////////////////////////////////////////////////////////////////
            Decorator::~Decorator()
            {
                delete m_pComponent;
                m_pComponent = NULL;
            }
            void ConCreateComponent::Operation()
            {
                std::cout 
            << "Operation of ConCreateComponent\n";
            }
            void ConCreateDecorator::Operation()
            {
                m_pComponent-
            >Operation();
                AddedBehavior();
            }
            void ConCreateDecorator::AddedBehavior()
            {
                std::cout 
            << "AddedBehavior of ConCreateDecorator\n";
            }
            //////////////////////////////////////////////////////////////////////////
            int main(int argc, char* argv[])
            {
                Component* Pcomponent 
            = new ConCreateComponent;
                //用這個對象去初始化一個Decorator對象
                //通過多態調用動態添加了職責
                Decorator* pDecorator 
            = new ConCreateDecorator(Pcomponent);
                pDecorator-
            >Operation();
                
                delete pDecorator;

                system("pause");
                return 0;
            }
            posted on 2008-08-18 22:45 黑色天使 閱讀(437) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式

            <2008年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            99精品久久精品| 国产精品无码久久四虎| 成人久久免费网站| 欧美精品久久久久久久自慰| 成人综合伊人五月婷久久| 99久久国产主播综合精品| 一级做a爰片久久毛片毛片| 久久AV高清无码| 欧美性大战久久久久久| 久久99国产精品二区不卡| 亚洲国产一成久久精品国产成人综合| 97精品伊人久久大香线蕉| 久久精品国产亚洲欧美| 久久SE精品一区二区| 久久中文字幕无码专区| 国产成人精品久久免费动漫| 精品久久久久久无码不卡| 品成人欧美大片久久国产欧美...| 欧美日韩精品久久久免费观看| 国产精品无码久久久久| 1000部精品久久久久久久久| 99久久香蕉国产线看观香| 久久久WWW成人免费毛片| 久久精品视频免费| 久久亚洲美女精品国产精品| 亚洲а∨天堂久久精品9966| 国产午夜电影久久| a级成人毛片久久| 99999久久久久久亚洲| 中文国产成人精品久久不卡| 久久无码中文字幕东京热| 亚州日韩精品专区久久久| 亚洲国产一成久久精品国产成人综合| 国产AⅤ精品一区二区三区久久| 国产午夜福利精品久久2021| 久久久久亚洲精品天堂| 欧美va久久久噜噜噜久久| 久久久久99精品成人片试看| 久久久国产精品亚洲一区| 久久国产色AV免费观看| 国产精品久久久久AV福利动漫|