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

            最新評論

            閱讀排行榜

            亚洲综合精品香蕉久久网97| 99久久精品国产一区二区| 久久97久久97精品免视看秋霞| 久久e热在这里只有国产中文精品99 | 一本久久精品一区二区| 国产色综合久久无码有码| 久久久久夜夜夜精品国产| 久久大香萑太香蕉av| 久久精品草草草| 午夜精品久久久久久中宇| 青青青青久久精品国产h| 久久久久高潮综合影院| 国产精品一区二区久久精品无码 | 精品99久久aaa一级毛片| 久久精品国产乱子伦| 久久精品一区二区影院| 久久香蕉国产线看观看精品yw| 四虎影视久久久免费| 四虎国产精品免费久久久| 久久成人国产精品| 久久天天躁狠狠躁夜夜avapp| 91亚洲国产成人久久精品| 狠狠88综合久久久久综合网 | 久久精品国产亚洲av高清漫画| 色青青草原桃花久久综合| 国产精品成人久久久久三级午夜电影 | 亚洲精品高清国产一久久| 亚洲va中文字幕无码久久不卡| 久久天天躁狠狠躁夜夜2020| 久久综合欧美成人| 国产99精品久久| 99久久婷婷国产综合亚洲| 伊人久久综合成人网| 一本一本久久A久久综合精品| 一级A毛片免费观看久久精品| 久久综合久久伊人| 久久综合视频网站| 久久99这里只有精品国产| 久久夜色精品国产噜噜亚洲a| 亚洲午夜福利精品久久| 久久99精品久久久久久9蜜桃|