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

            搜索

            •  

            積分與排名

            • 積分 - 1804430
            • 排名 - 5

            最新評論

            閱讀排行榜

            日韩精品久久久久久| 99久久国产精品免费一区二区| 国产欧美久久久精品影院| 99热精品久久只有精品| 人妻精品久久无码区| 久久久噜噜噜久久中文福利| 久久人妻无码中文字幕| 亚洲日本va中文字幕久久| 久久久久久久久久久精品尤物| 久久久久亚洲AV无码观看| 亚洲AV伊人久久青青草原| 天天影视色香欲综合久久| 久久精品亚洲男人的天堂| 四虎影视久久久免费观看| 国产亚洲精品久久久久秋霞 | 久久97久久97精品免视看| 狠狠色综合网站久久久久久久| 久久99热这里只有精品国产| 午夜视频久久久久一区 | 久久九九久精品国产| 人人狠狠综合久久亚洲| 亚洲欧洲日产国码无码久久99| 久久久久久久亚洲Av无码| 国产成人99久久亚洲综合精品| 国产免费久久精品丫丫| 伊人 久久 精品| 嫩草影院久久国产精品| 日韩中文久久| 国产亚洲美女精品久久久久狼| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 国产激情久久久久影院| 色狠狠久久综合网| 久久精品蜜芽亚洲国产AV| 亚洲一区中文字幕久久| 精品国产乱码久久久久久呢| 99久久精品国产综合一区| 偷偷做久久久久网站| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 久久夜色撩人精品国产| 国产精品久久亚洲不卡动漫| 久久精品国产一区二区电影|