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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品成人欧美大片| 久久精品极品盛宴观看| 久久精品国产亚洲av水果派| 久久精品99久久香蕉国产色戒| 大伊人青草狠狠久久| 久久亚洲中文字幕精品一区| 亚洲精品无码久久一线| 青青青伊人色综合久久| 波多野结衣久久| 久久精品视频网| 亚洲国产精品成人久久| 久久嫩草影院免费看夜色| 久久久久99精品成人片试看| 欧美精品福利视频一区二区三区久久久精品 | 久久国产精品-久久精品| 久久久久综合中文字幕| 久久99亚洲网美利坚合众国| 亚洲精品tv久久久久| 久久精品二区| 久久精品国产精品国产精品污| 97香蕉久久夜色精品国产| 久久久久97国产精华液好用吗| 狠狠色婷婷久久一区二区三区| 精品国产乱码久久久久软件| 久久午夜福利电影| 久久99精品久久久久久噜噜| 久久综合狠狠综合久久激情 | 久久久精品人妻一区二区三区蜜桃| 中文字幕精品久久| 伊人色综合久久天天人守人婷| 久久国产一片免费观看| 99久久国产热无码精品免费久久久久 | 精品一区二区久久久久久久网站| 中文国产成人精品久久亚洲精品AⅤ无码精品| 久久国产一区二区| 久久精品国产91久久综合麻豆自制| 99久久精品免费看国产一区二区三区| 国产精品亚洲综合久久| 欧美亚洲国产精品久久久久| 国产香蕉久久精品综合网| 国产精品99久久久精品无码 |