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


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


            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            香蕉99久久国产综合精品宅男自 | 亚洲嫩草影院久久精品| 7777久久亚洲中文字幕| 国产精品九九久久免费视频 | 久久一本综合| 国产精品天天影视久久综合网| 成人妇女免费播放久久久| 久久久久久国产精品美女| 久久精品国产亚洲沈樵| 久久天堂AV综合合色蜜桃网| 久久久久亚洲AV成人网人人软件| 亚洲精品无码久久毛片| 久久97久久97精品免视看秋霞| 狠狠88综合久久久久综合网 | 久久久久久狠狠丁香| 久久久久亚洲AV无码专区体验| 久久精品国产男包| 九九久久精品国产| 久久婷婷五月综合97色一本一本 | 精品精品国产自在久久高清 | 久久国产欧美日韩精品| 久久免费视频1| 久久人人爽人爽人人爽av| 欧美综合天天夜夜久久| 国产三级久久久精品麻豆三级| 中文字幕亚洲综合久久菠萝蜜| 一级做a爰片久久毛片16| 久久久噜噜噜www成人网| 久久精品国产乱子伦| 久久人做人爽一区二区三区| 欧洲国产伦久久久久久久| 久久精品无码一区二区三区| 97久久精品无码一区二区| 色婷婷综合久久久久中文 | 久久精品亚洲精品国产欧美| 国内精品久久久久久久coent| 久久99国产亚洲高清观看首页| 久久亚洲AV成人出白浆无码国产| 日本欧美久久久久免费播放网| 国内精品综合久久久40p| 久久久久久综合一区中文字幕|