• <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 夢在天涯 閱讀(770) 評論(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

            搜索

            •  

            積分與排名

            • 積分 - 1807518
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久九九亚洲精品| 狠狠狠色丁香婷婷综合久久五月| 一级做a爰片久久毛片16| 国产精品无码久久久久| 亚洲国产成人久久一区WWW| 日韩久久久久久中文人妻| 日本一区精品久久久久影院| 欧美日韩精品久久免费| 久久天堂电影网| 三级三级久久三级久久| 国产成人精品久久亚洲| 伊人久久大香线蕉av不卡| 精品久久久久久久中文字幕 | 久久精品中文字幕无码绿巨人| 无码国产69精品久久久久网站| 久久精品成人一区二区三区| 亚洲精品乱码久久久久久中文字幕| 一本大道久久a久久精品综合| 99久久香蕉国产线看观香| 91精品久久久久久无码| 中文字幕久久久久人妻| 亚洲欧洲精品成人久久曰影片| 免费观看久久精彩视频| 久久99国产综合精品女同| 久久AV高潮AV无码AV| 三级韩国一区久久二区综合| 91久久精品国产成人久久| 91久久精一区二区三区大全| 伊人久久大香线蕉av一区| 国产精品久久久久久久人人看| 久久嫩草影院免费看夜色| 久久久久久亚洲精品无码| 婷婷久久综合九色综合98| 精品国产VA久久久久久久冰| 人妻无码αv中文字幕久久琪琪布| 久久久久久国产精品无码下载| 天天综合久久一二三区| 日韩精品久久久久久久电影| 久久精品青青草原伊人| 亚洲精品无码专区久久久| 少妇内射兰兰久久|