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

            linux&c++ R&D

            programing is a pleasure!

            Understand decorator design pattern

                The Decorator Pattern is used for adding additional functionality to a particular object as opposed to a class of objects. It is easy to add functionality to an entire class of objects by subclassing an object, but it is impossible to extend a single object this way. With the Decorator Pattern, you can add functionality to a single object and leave others like it unmodified.
               A Decorator, also known as a Wrapper, is an object that has an interface identical to an object that it contains. Any calls that the decorator gets, it relays to the object that it contains, and adds its own functionality along the way, either before or after the call. This gives you a lot of flexibility, since you can change what the decorator does at runtime, as opposed to having the change be static and determined at compile time by subclassing. Since a Decorator complies with the interface that the object that it contains, the Decorator is indistinguishable from the object that it contains.  That is, a Decorator is a concrete instance of the abstract class, and thus is indistinguishable from any other concrete instance, including other decorators.   This can be used to great advantage, as you can recursively nest decorators without any other objects being able to tell the difference, allowing a near infinite amount of customization.



            An example:

            #include <iostream>
            class IntCompute{
            public:
                virtual 
            int Compute(int,int=0;
            }
            ;
            class IntComputeAdd:public IntCompute{
            public:
            int Compute(int a,int b){
                
            return a+b;
            }

            }
            ;
            class DecorateMinusAdd:public IntCompute{
            private:
                IntComputeAdd intadd;
            public:
                
            int Compute(int a,int b)
                
            {
                  
            return -(intadd.Compute(a,b));

                }

            }
            ;
            class DecorateSumSquare:public IntCompute{
            private:
                IntComputeAdd intadd;
            public:
                
            int Compute(int a,int b)
                
            {
                    
            int result=intadd.Compute(a,b);
                    
            return result*result;

                }

            }
            ;
            int main()
            {
             
            DecorateMinusAdd minadd;
            DecorateSumSquare sumsquare;
            std::cout
            <<minadd.Compute(10,20)<<std::endl;
            std::cout
            <<sumsquare.Compute(10,20)<<std::endl;
             
            return 0;
            }

            posted on 2007-05-11 21:38 丑石 閱讀(479) 評論(0)  編輯 收藏 引用 所屬分類: software engineering

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久国产欧美日韩精品| 欧美精品一本久久男人的天堂| 久久不见久久见免费视频7| 热久久国产欧美一区二区精品| 伊人色综合久久天天网| 久久男人中文字幕资源站| 人妻无码中文久久久久专区| 精品久久久久久亚洲精品| 久久成人精品| 国产成人精品久久免费动漫| 伊人久久大香线蕉综合5g| 久久免费国产精品一区二区| 人妻无码αv中文字幕久久琪琪布| 欧美黑人又粗又大久久久| 大蕉久久伊人中文字幕| 久久亚洲美女精品国产精品| 久久黄色视频| 亚洲一区中文字幕久久| 久久综合狠狠综合久久| 久久精品亚洲男人的天堂| 久久精品国产亚洲精品2020| 国产精品久久新婚兰兰| 国産精品久久久久久久| 久久久久四虎国产精品| 久久人人妻人人爽人人爽| 国产成人久久精品一区二区三区| 国产农村妇女毛片精品久久 | 久久婷婷人人澡人人爽人人爱| 久久热这里只有精品在线观看| 欧美亚洲另类久久综合| 国内精品久久久久影院薰衣草| 中文精品久久久久国产网址| 色诱久久久久综合网ywww| 日韩欧美亚洲综合久久| 久久强奷乱码老熟女网站| 精品无码人妻久久久久久| 狠狠色伊人久久精品综合网| 一本久久a久久精品综合夜夜| 精品久久久久久国产免费了| 一级做a爰片久久毛片16| 久久免费国产精品一区二区|