• <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#--行為型--mediator

            名稱 Mediator
            結構 o_mediator.bmp
            意圖 用一個中介對象來封裝一系列的對象交互。中介者使各對象不需要顯式地相互引用,從而使其耦合松散,而且可以獨立地改變它們之間的交互。
            適用性
            • 一組對象以定義良好但是復雜的方式進行通信。產生的相互依賴關系結構混亂且難以理解。
            • 一個對象引用其他很多對象并且直接與這些對象通信,導致難以復用該對象。
            • 想定制一個分布在多個類中的行為,而又不想生成太多的子類。


            namespace ?Mediator_DesignPattern
            {
            ????
            using ?System;

            ????
            class ?Mediator?
            ????
            {
            ????????
            private ?DataProviderColleague?dataProvider;
            ????????
            private ?DataConsumerColleague?dataConsumer;
            ????????
            public ? void ?IntroduceColleagues(DataProviderColleague?c1,?DataConsumerColleague?c2)
            ????????
            {
            ????????????dataProvider?
            = ?c1;
            ????????????dataConsumer?
            = ?c2;????????????
            ????????}

            ????????
            ????????
            public ? void ?DataChanged()
            ????????
            {
            ????????????
            int ?i? = ?dataProvider.MyData;
            ????????????dataConsumer.NewValue(i);
            ????????}

            ????}


            ????
            class ?DataConsumerColleague?
            ????
            {
            ????????
            public ? void ?NewValue( int ?i)
            ????????
            {
            ????????????Console.WriteLine(
            " New?value?{0} " ,?i);
            ????????}

            ????}


            ????
            class ?DataProviderColleague
            ????
            {
            ????????
            private ?Mediator?mediator;
            ????????
            private ? int ?iMyData = 0 ;
            ????????
            public ? int ?MyData?
            ????????
            {
            ????????????
            get ?
            ????????????
            {
            ????????????????
            return ?iMyData;
            ????????????}

            ????????????
            set ?
            ????????????
            {
            ????????????????iMyData?
            = ?value;
            ????????????}

            ????????}

            ????????
            public ?DataProviderColleague(Mediator?m)
            ????????
            {
            ????????????mediator?
            = ?m;
            ????????}


            ????????
            public ? void ?ChangeData()
            ????????
            {
            ????????????iMyData?
            = ? 403 ;

            ????????????
            // ?Inform?mediator?that?I?have?changed?the?data
            ???????????? if ?(mediator? != ? null )
            ????????????????mediator.DataChanged();????
            ????????}
            ????????
            ????}


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

            ???? public ? class ?Client
            ????
            {
            ????????
            public ? static ? int ?Main( string []?args)
            ????????
            {????????????
            ????????????Mediator?m?
            = ? new ?Mediator();
            ????????????DataProviderColleague?c1?
            = ? new ?DataProviderColleague(m);
            ????????????DataConsumerColleague?c2?
            = ? new ?DataConsumerColleague();
            ????????????m.IntroduceColleagues(c1,c2);

            ????????????c1.ChangeData();

            ????????????
            return ? 0 ;
            ????????}

            ????}

            }

            很好的例子:聊天室:

            //?Mediator?pattern?--?Real?World?example??


            using?System;
            using?System.Collections;

            namespace?DoFactory.GangOfFour.Mediator.RealWorld
            {
            ??
            ??
            //?MainApp?test?application?

            ??
            class?MainApp
            ??
            {
            ????
            static?void?Main()
            ????
            {
            ??????
            //?Create?chatroom?
            ??????Chatroom?chatroom?=?new?Chatroom();

            ??????
            //?Create?participants?and?register?them?
            ??????Participant?George?=?new?Beatle("George");
            ??????Participant?Paul?
            =?new?Beatle("Paul");
            ??????Participant?Ringo?
            =?new?Beatle("Ringo");
            ??????Participant?John?
            =?new?Beatle("John")?;
            ??????Participant?Yoko?
            =?new?NonBeatle("Yoko");

            ??????chatroom.Register(George);
            ??????chatroom.Register(Paul);
            ??????chatroom.Register(Ringo);
            ??????chatroom.Register(John);
            ??????chatroom.Register(Yoko);

            ??????
            //?Chatting?participants?
            ??????Yoko.Send?("John",?"Hi?John!");
            ??????Paul.Send?(
            "Ringo",?"All?you?need?is?love");
            ??????Ringo.Send(
            "George",?"My?sweet?Lord");
            ??????Paul.Send?(
            "John",?"Can't?buy?me?love");
            ??????John.Send?(
            "Yoko",?"My?sweet?love")?;

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

            ??}


            ??
            //?"Mediator"?

            ??
            abstract?class?AbstractChatroom
            ??
            {
            ????
            public?abstract?void?Register(Participant?participant);
            ????
            public?abstract?void?Send(
            ??????
            string?from,?string?to,?string?message);
            ??}


            ??
            //?"ConcreteMediator"?

            ??
            class?Chatroom?:?AbstractChatroom
            ??
            {
            ????
            private?Hashtable?participants?=?new?Hashtable();

            ????
            public?override?void?Register(Participant?participant)
            ????
            {
            ??????
            if?(participants[participant.Name]?==?null)
            ??????
            {
            ????????participants[participant.Name]?
            =?participant;
            ??????}


            ??????participant.Chatroom?
            =?this;
            ????}


            ????
            public?override?void?Send(
            ??????
            string?from,?string?to,?string?message)
            ????
            {
            ??????Participant?pto?
            =?(Participant)participants[to];
            ??????
            if?(pto?!=?null)
            ??????
            {
            ????????pto.Receive(from,?message);
            ??????}

            ????}

            ??}


            ??
            //?"AbstractColleague"?

            ??
            class?Participant
            ??
            {
            ????
            private?Chatroom?chatroom;
            ????
            private?string?name;

            ????
            //?Constructor?
            ????public?Participant(string?name)
            ????
            {
            ??????
            this.name?=?name;
            ????}


            ????
            //?Properties?
            ????public?string?Name
            ????
            {
            ??????
            get{?return?name;?}
            ????}


            ????
            public?Chatroom?Chatroom
            ????
            {
            ??????
            set{?chatroom?=?value;?}
            ??????
            get{?return?chatroom;?}
            ????}


            ????
            public?void?Send(string?to,?string?message)
            ????
            {
            ??????chatroom.Send(name,?to,?message);
            ????}


            ????
            public?virtual?void?Receive(
            ??????
            string?from,?string?message)
            ????
            {
            ??????Console.WriteLine(
            "{0}?to?{1}:?'{2}'",
            ????????from,?Name,?message);
            ????}

            ??}


            ??
            //"?ConcreteColleague1"?

            ??
            class?Beatle?:?Participant
            ??
            {
            ????
            //?Constructor?
            ????public?Beatle(string?name)?:?base(name)?
            ????
            {?
            ????}


            ????
            public?override?void?Receive(string?from,?string?message)
            ????
            {
            ??????Console.Write(
            "To?a?Beatle:?");
            ??????
            base.Receive(from,?message);
            ????}

            ??}


            ??
            //"?ConcreteColleague2"?

            ??
            class?NonBeatle?:?Participant
            ??
            {
            ????
            //?Constructor?
            ????public?NonBeatle(string?name)?:?base(name)?
            ????
            {?
            ????}


            ????
            public?override?void?Receive(string?from,?string?message)
            ????
            {
            ??????Console.Write(
            "To?a?non-Beatle:?");
            ??????
            base.Receive(from,?message);
            ????}

            ??}

            }

            ?

            posted on 2006-01-03 16:10 夢在天涯 閱讀(735) 評論(0)  編輯 收藏 引用 所屬分類: Design pattern

            公告

            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

            最新評論

            閱讀排行榜

            久久久久久免费一区二区三区| 久久人人爽人人爽人人AV| 久久夜色精品国产亚洲| 久久精品国产91久久麻豆自制| 久久电影网一区| 色欲综合久久躁天天躁| 欧美伊人久久大香线蕉综合| 无码人妻久久一区二区三区免费丨| 久久综合狠狠综合久久| 久久99亚洲综合精品首页| 色狠狠久久综合网| 久久亚洲欧美日本精品| 中文字幕无码免费久久| 99国内精品久久久久久久| 人人妻久久人人澡人人爽人人精品| 久久免费的精品国产V∧| 欧美精品一区二区久久| 国产V亚洲V天堂无码久久久| 欧美精品一区二区久久| 国产精品欧美亚洲韩国日本久久| 精品久久亚洲中文无码| 久久久久久国产a免费观看不卡| 色婷婷久久综合中文久久蜜桃av| 久久精品亚洲福利| 久久精品成人免费看| 77777亚洲午夜久久多人| 亚洲AV伊人久久青青草原| 九九99精品久久久久久| AV无码久久久久不卡蜜桃| 久久播电影网| 久久久久久久尹人综合网亚洲| 亚洲精品国产美女久久久| 99久久国产亚洲综合精品| 欧美日韩成人精品久久久免费看 | 无码任你躁久久久久久久| 久久精品人人做人人爽电影| 欧美喷潮久久久XXXXx| 无码八A片人妻少妇久久| 国产精品99久久久久久宅男| 精品久久久久久国产免费了| 精品久久久久香蕉网|