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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久久精品免费国产四虎| 香蕉久久夜色精品国产小说| 91精品国产91热久久久久福利| 国产精品久久自在自线观看| 国产日产久久高清欧美一区| 欧美精品一本久久男人的天堂| 国产激情久久久久影院老熟女免费| 国产免费久久精品丫丫| 久久人人爽人人澡人人高潮AV | 国产精品女同一区二区久久| 丰满少妇人妻久久久久久4| 久久99精品久久久久久水蜜桃| 亚洲综合日韩久久成人AV| 72种姿势欧美久久久久大黄蕉| 久久国产香蕉视频| 久久久久久久久无码精品亚洲日韩| 99久久99久久精品国产片| 久久亚洲日韩看片无码| 国产成人久久精品麻豆一区| 久久久久人妻一区二区三区| 国产精品免费久久久久久久久| 亚洲AV无一区二区三区久久| 国内精品久久久久久久coent| 亚洲精品乱码久久久久久按摩| 久久久久久久综合综合狠狠| 伊人精品久久久久7777| 国产香蕉久久精品综合网| 久久精品国产69国产精品亚洲| 一本一道久久综合狠狠老| 久久久久国色AV免费观看| 久久福利青草精品资源站免费| 亚洲欧美伊人久久综合一区二区 | 久久精品成人免费看| 久久综合亚洲欧美成人| 97久久婷婷五月综合色d啪蜜芽| 欧美亚洲日本久久精品| 成人a毛片久久免费播放| 1000部精品久久久久久久久| 亚洲成色WWW久久网站| 亚洲乱码中文字幕久久孕妇黑人| 综合久久给合久久狠狠狠97色 |