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

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            模式設(shè)計(jì)c#--結(jié)構(gòu)型--decorator

            名稱 Decorator
            結(jié)構(gòu) o_decorator.bmp
            意圖 動(dòng)態(tài)地給一個(gè)對(duì)象添加一些額外的職責(zé)。就增加功能來說,D e c o r a t o r 模式相比生成子類更為靈活。
            適用性
            • 在不影響其他對(duì)象的情況下,以動(dòng)態(tài)、透明的方式給單個(gè)對(duì)象添加職責(zé)。
            • 處理那些可以撤消的職責(zé)。
            • 當(dāng)不能采用生成子類的方法進(jìn)行擴(kuò)充時(shí)。一種情況是,可能有大量獨(dú)立的擴(kuò)展,為支持每一種組合將產(chǎn)生大量的子類,使得子類數(shù)目呈爆炸性增長(zhǎng)。另一種情況可能是因?yàn)轭惗x被隱藏,或類定義不能用于生成子類。

            Code Example
            namespace?Decorator_DesignPattern
            {
            ????
            using?System;

            ????
            abstract?class?Component
            ????
            {
            ????????
            public?abstract?void?Draw();?????????
            ????}


            ????
            class?ConcreteComponent?:?Component
            ????
            {
            ????????
            private?string?strName;
            ????????
            public?ConcreteComponent(string?s)
            ????????
            {
            ????????????strName?
            =?s;????????????
            ????????}


            ????????
            public?override?void?Draw()
            ????????
            {
            ????????????Console.WriteLine(
            "ConcreteComponent?-?{0}",?strName);????????????
            ????????}
            ????????
            ????}


            ????
            abstract?class?Decorator?:?Component
            ????
            {
            ????????
            protected?Component?ActualComponent;

            ????????
            public?void?SetComponent(Component?c)
            ????????
            {
            ????????????ActualComponent?
            =?c;
            ????????}

            ????????
            public?override?void?Draw()
            ????????
            {
            ????????????
            if?(ActualComponent?!=?null)
            ????????????????ActualComponent.Draw();????????
            ????????}

            ????}


            ????
            class?ConcreteDecorator?:?Decorator?
            ????
            {
            ????????
            private?string?strDecoratorName;
            ????????
            public?ConcreteDecorator?(string?str)
            ????????
            {
            ????????????
            //?how?decoration?occurs?is?localized?inside?this?decorator
            ????????????
            //?For?this?demo,?we?simply?print?a?decorator?name
            ????????????strDecoratorName?=?str;?
            ????????}

            ????????
            public?override?void?Draw()
            ????????
            {
            ????????????CustomDecoration();
            ????????????
            base.Draw();
            ????????}

            ????????
            void?CustomDecoration()
            ????????
            {
            ????????????Console.WriteLine(
            "In?ConcreteDecorator:?decoration?goes?here");
            ????????????Console.WriteLine(
            "{0}",?strDecoratorName);
            ????????}

            ????}


            ????
            ????
            ///?<summary>
            ????
            ///????Summary?description?for?Client.
            ????
            ///?</summary>

            ????public?class?Client
            ????
            {
            ????????Component?Setup()?
            ????????
            {
            ????????????ConcreteComponent?c?
            =?new?ConcreteComponent("This?is?the?real?component");

            ????????????ConcreteDecorator?d?
            =?new?ConcreteDecorator("This?is?a?decorator?for?the?component");

            ????????????d.SetComponent(c);

            ????????????
            return?d;
            ????????}

            ????????
            ????????
            public?static?int?Main(string[]?args)
            ????????
            {
            ????????????Client?client?
            =?new?Client();
            ????????????Component?c?
            =?client.Setup();????

            ????????????
            //?The?code?below?will?work?equally?well?with?the?real?component,?
            ????????????
            //?or?a?decorator?for?the?component

            ????????????c.Draw();
            ????????????
            ????????????
            return?0;
            ????????}

            ????}

            }


            更好的例子區(qū)別于代理模式:
            裝飾模式,對(duì)相同性質(zhì)的東東的更好的一個(gè)抽象.
            代理模式只是代理,所代理的對(duì)象可以有類的父子關(guān)系,但是只有一個(gè)類對(duì)象.
            //?Decorator?pattern?--?Real?World?example??


            using?System;
            using?System.Collections;

            namespace?DoFactory.GangOfFour.Decorator.RealWorld
            {

            ??
            //?MainApp?test?application?

            ??
            class?MainApp
            ??
            {
            ????
            static?void?Main()
            ????
            {
            ??????
            //?Create?book?
            ??????Book?book?=?new?Book?("Worley",?"Inside?ASP.NET",?10);
            ??????book.Display();

            ??????
            //?Create?video?
            ??????Video?video?=?new?Video?("Spielberg",?"Jaws",?23,?92);
            ??????video.Display();

            ??????
            //?Make?video?borrowable,?then?borrow?and?display?
            ??????Console.WriteLine("\nMaking?video?borrowable:");

            ??????Borrowable?borrowvideo?
            =?new?Borrowable(video);
            ??????borrowvideo.BorrowItem(
            "Customer?#1");
            ??????borrowvideo.BorrowItem(
            "Customer?#2");

            ??????borrowvideo.Display();

            ??????
            //?Wait?for?user?
            ??????Console.Read();
            ????}

            ??}


            ??
            //?"Component"?

            ??
            abstract?class?LibraryItem
            ??
            {
            ????
            private?int?numCopies;

            ????
            //?Property?
            ????public?int?NumCopies
            ????
            {
            ??????
            get{?return?numCopies;?}
            ??????
            set{?numCopies?=?value;?}
            ????}


            ????
            public?abstract?void?Display();
            ??}


            ??
            //?"ConcreteComponent"?

            ??
            class?Book?:?LibraryItem
            ??
            {
            ????
            private?string?author;
            ????
            private?string?title;

            ????
            //?Constructor?
            ????public?Book(string?author,string?title,int?numCopies)
            ????
            {
            ??????
            this.author?=?author;
            ??????
            this.title?=?title;
            ??????
            this.NumCopies?=?numCopies;
            ????}


            ????
            public?override?void?Display()
            ????
            {
            ??????Console.WriteLine(
            "\nBook?------?");
            ??????Console.WriteLine(
            "?Author:?{0}",?author);
            ??????Console.WriteLine(
            "?Title:?{0}",?title);
            ??????Console.WriteLine(
            "?#?Copies:?{0}",?NumCopies);
            ????}

            ??}


            ??
            //?"ConcreteComponent"?

            ??
            class?Video?:?LibraryItem
            ??
            {
            ????
            private?string?director;
            ????
            private?string?title;
            ????
            private?int?playTime;

            ????
            //?Constructor?
            ????public?Video(string?director,?string?title,?
            ??????
            int?numCopies,?int?playTime)
            ????
            {
            ??????
            this.director?=?director;
            ??????
            this.title?=?title;
            ??????
            this.NumCopies?=?numCopies;
            ??????
            this.playTime?=?playTime;
            ????}


            ????
            public?override?void?Display()
            ????
            {
            ??????Console.WriteLine(
            "\nVideo?-----?");
            ??????Console.WriteLine(
            "?Director:?{0}",?director);
            ??????Console.WriteLine(
            "?Title:?{0}",?title);
            ??????Console.WriteLine(
            "?#?Copies:?{0}",?NumCopies);
            ??????Console.WriteLine(
            "?Playtime:?{0}\n",?playTime);
            ????}

            ??}


            ??
            //?"Decorator"?

            ??
            abstract?class?Decorator?:?LibraryItem
            ??
            {
            ????
            protected?LibraryItem?libraryItem;

            ????
            //?Constructor?
            ????public?Decorator(LibraryItem?libraryItem)
            ????
            {
            ??????
            this.libraryItem?=?libraryItem;
            ????}


            ????
            public?override?void?Display()
            ????
            {
            ??????libraryItem.Display();
            ????}

            ??}


            ??
            //?"ConcreteDecorator"?

            ??
            class?Borrowable?:?Decorator
            ??
            {
            ????
            protected?ArrayList?borrowers?=?new?ArrayList();

            ????
            //?Constructor?
            ????public?Borrowable(LibraryItem?libraryItem)?
            ??????:?
            base(libraryItem)?
            ????
            {
            ????}


            ????
            public?void?BorrowItem(string?name)
            ????
            {
            ??????borrowers.Add(name);
            ??????libraryItem.NumCopies
            --;
            ????}


            ????
            public?void?ReturnItem(string?name)
            ????
            {
            ??????borrowers.Remove(name);
            ??????libraryItem.NumCopies
            ++;
            ????}


            ????
            public?override?void?Display()
            ????
            {
            ??????
            base.Display();
            ??????
            ??????
            foreach?(string?borrower?in?borrowers)
            ??????
            {
            ????????Console.WriteLine(
            "?borrower:?"?+?borrower);
            ??????}

            ????}

            ??}

            }

            ?

            Output
            Book ------
            Author: Worley
            Title: Inside ASP.NET
            # Copies: 10

            Video -----
            Director: Spielberg
            Title: Jaws
            # Copies: 23
            Playtime: 92


            Making video borrowable:

            Video -----
            Director: Spielberg
            Title: Jaws
            # Copies: 21
            Playtime: 92

            borrower: Customer #1
            borrower: Customer #2

            posted on 2006-01-03 15:47 夢(mèng)在天涯 閱讀(770) 評(píng)論(3)  編輯 收藏 引用 所屬分類: Design pattern

            評(píng)論

            # re: 模式設(shè)計(jì)c#--結(jié)構(gòu)型--decorator 2006-04-24 15:04 夢(mèng)在天涯

            裝飾(Decorator)模式又名包裝(Wrapper)模式[GOF95]。裝飾模式以對(duì)客戶端透明的方式擴(kuò)展對(duì)象的功能,是繼承關(guān)系的一個(gè)替代方案。  回復(fù)  更多評(píng)論   

            # re: 模式設(shè)計(jì)c#--結(jié)構(gòu)型--decorator 2006-04-24 15:10 夢(mèng)在天涯

            裝飾模式應(yīng)當(dāng)在什么情況下使用
            在以下情況下應(yīng)當(dāng)使用裝飾模式:

            需要擴(kuò)展一個(gè)類的功能,或給一個(gè)類增加附加責(zé)任。
            需要?jiǎng)討B(tài)地給一個(gè)對(duì)象增加功能,這些功能可以再動(dòng)態(tài)地撤銷。
            需要增加由一些基本功能的排列組合而產(chǎn)生的非常大量的功能,從而使繼承關(guān)系變得不現(xiàn)實(shí)。  回復(fù)  更多評(píng)論   

            # re: 模式設(shè)計(jì)c#--結(jié)構(gòu)型--decorator 2006-04-24 15:14 夢(mèng)在天涯

            使用裝飾模式的優(yōu)點(diǎn)和缺點(diǎn)
            使用裝飾模式主要有以下的優(yōu)點(diǎn):

            裝飾模式與繼承關(guān)系的目的都是要擴(kuò)展對(duì)象的功能,但是裝飾模式可以提供比繼承更多的靈活性。
            通過使用不同的具體裝飾類以及這些裝飾類的排列組合,設(shè)計(jì)師可以創(chuàng)造出很多不同行為的組合。
            這種比繼承更加靈活機(jī)動(dòng)的特性,也同時(shí)意味著裝飾模式比繼承更加易于出錯(cuò)。
            使用裝飾模式主要有以下的缺點(diǎn):

            由于使用裝飾模式,可以比使用繼承關(guān)系需要較少數(shù)目的類。使用較少的類,當(dāng)然使設(shè)計(jì)比較易于進(jìn)行。但是,在另一方面,使用裝飾模式會(huì)產(chǎn)生比使用繼承關(guān)系更多的對(duì)象。更多的對(duì)象會(huì)使得查錯(cuò)變得困難,特別是這些對(duì)象看上去都很相像。

              回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807503
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            色偷偷88欧美精品久久久| 亚洲成色999久久网站| 日批日出水久久亚洲精品tv| 久久人人爽人人精品视频| 思思久久99热只有频精品66| 99久久精品国内| 色天使久久综合网天天 | 国产精品久久波多野结衣| 久久久91人妻无码精品蜜桃HD| 久久精品国产欧美日韩99热| 精品久久久久久久久中文字幕| 午夜精品久久久内射近拍高清 | 久久久久人妻精品一区| 国产精品免费久久| 久久婷婷五月综合国产尤物app| 久久精品亚洲福利| 久久66热人妻偷产精品9| 无码人妻久久一区二区三区蜜桃| 品成人欧美大片久久国产欧美| 亚洲AV无码久久精品色欲| 久久男人AV资源网站| 伊人色综合久久天天| 国内精品人妻无码久久久影院 | 人妻无码久久一区二区三区免费| 日韩精品无码久久一区二区三| 一本久久久久久久| 国产成人精品久久免费动漫| 伊人久久大香线蕉av不变影院| 午夜视频久久久久一区| 久久久WWW免费人成精品| 99久久精品免费看国产免费| 精品国产VA久久久久久久冰| 久久久久高潮毛片免费全部播放| 久久国产劲爆AV内射—百度| 色婷婷久久久SWAG精品| 一本色道久久综合| 久久精品青青草原伊人| 亚洲精品乱码久久久久久中文字幕| 丁香色欲久久久久久综合网| 99久久国产综合精品女同图片| 亚洲人成精品久久久久|