• <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++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            模式設計c#--結構型--decorator

            名稱 Decorator
            結構 o_decorator.bmp
            意圖 動態地給一個對象添加一些額外的職責。就增加功能來說,D e c o r a t o r 模式相比生成子類更為靈活。
            適用性
            • 在不影響其他對象的情況下,以動態、透明的方式給單個對象添加職責。
            • 處理那些可以撤消的職責。
            • 當不能采用生成子類的方法進行擴充時。一種情況是,可能有大量獨立的擴展,為支持每一種組合將產生大量的子類,使得子類數目呈爆炸性增長。另一種情況可能是因為類定義被隱藏,或類定義不能用于生成子類。

            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;
            ????????}

            ????}

            }


            更好的例子區別于代理模式:
            裝飾模式,對相同性質的東東的更好的一個抽象.
            代理模式只是代理,所代理的對象可以有類的父子關系,但是只有一個類對象.
            //?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 夢在天涯 閱讀(764) 評論(3)  編輯 收藏 引用 所屬分類: Design pattern

            評論

            # re: 模式設計c#--結構型--decorator 2006-04-24 15:04 夢在天涯

            裝飾(Decorator)模式又名包裝(Wrapper)模式[GOF95]。裝飾模式以對客戶端透明的方式擴展對象的功能,是繼承關系的一個替代方案。  回復  更多評論   

            # re: 模式設計c#--結構型--decorator 2006-04-24 15:10 夢在天涯

            裝飾模式應當在什么情況下使用
            在以下情況下應當使用裝飾模式:

            需要擴展一個類的功能,或給一個類增加附加責任。
            需要動態地給一個對象增加功能,這些功能可以再動態地撤銷。
            需要增加由一些基本功能的排列組合而產生的非常大量的功能,從而使繼承關系變得不現實。  回復  更多評論   

            # re: 模式設計c#--結構型--decorator 2006-04-24 15:14 夢在天涯

            使用裝飾模式的優點和缺點
            使用裝飾模式主要有以下的優點:

            裝飾模式與繼承關系的目的都是要擴展對象的功能,但是裝飾模式可以提供比繼承更多的靈活性。
            通過使用不同的具體裝飾類以及這些裝飾類的排列組合,設計師可以創造出很多不同行為的組合。
            這種比繼承更加靈活機動的特性,也同時意味著裝飾模式比繼承更加易于出錯。
            使用裝飾模式主要有以下的缺點:

            由于使用裝飾模式,可以比使用繼承關系需要較少數目的類。使用較少的類,當然使設計比較易于進行。但是,在另一方面,使用裝飾模式會產生比使用繼承關系更多的對象。更多的對象會使得查錯變得困難,特別是這些對象看上去都很相像。

              回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804390
            • 排名 - 5

            最新評論

            閱讀排行榜

            亚洲va久久久噜噜噜久久天堂| 97久久精品人人澡人人爽| 国产福利电影一区二区三区久久老子无码午夜伦不 | 国内精品久久久久久久久电影网| 久久伊人精品青青草原高清| 国产午夜福利精品久久| 国产精品久久久久久| 四虎亚洲国产成人久久精品| 久久无码高潮喷水| 久久被窝电影亚洲爽爽爽| 亚洲人成电影网站久久| 精品久久久久久中文字幕人妻最新| 狠狠色综合网站久久久久久久 | 久久精品无码一区二区日韩AV| 久久精品国产亚洲av麻豆蜜芽| 国产精品久久久久影视不卡| 精品久久人妻av中文字幕| 久久国产成人精品麻豆| 夜夜亚洲天天久久| 亚洲国产成人久久综合碰| 久久精品国产亚洲一区二区| 7国产欧美日韩综合天堂中文久久久久 | 狠狠久久综合| 伊人久久大香线蕉AV一区二区| 久久精品一本到99热免费| 久久精品蜜芽亚洲国产AV| 亚洲人成无码久久电影网站| 久久久久久精品久久久久| 久久久久久无码Av成人影院| 国产精品成人99久久久久91gav| 欧美久久久久久精选9999| 久久99国产综合精品| 91久久精品电影| 中文字幕久久精品无码| 伊人 久久 精品| 国产精品美女久久久久| 亚洲国产成人久久综合碰| 久久99国产精品久久久| 无码8090精品久久一区| 97r久久精品国产99国产精| 久久久久亚洲AV无码专区桃色 |