设计模式——中介者模式

名称Mediator
结构 Mediator.gif
意图用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
适用性
  • 一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
  • 一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
  • 想定制一个分布在多个类中的行为,而又不想生成太多的子类。
Code Example
 1 None.gif //  Mediator
 2 None.gif
 3 None.gif //  Intent: "Define an object that encapsulates how a set of objects interact. 
 4 None.gif //  Mediator promotes loose coupling by keeping objects from referring to each
 5 None.gif //  other explicitly, and it lets you vary their interaction independently." 
 6 None.gif
 7 None.gif //  For further information, read "Design Patterns", p273, Gamma et al.,
 8 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
 9 None.gif
10 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
11InBlock.gif * Consider a mediator as a hub, which objects that need to talk -
12InBlock.gif * but do not wish to be interdependent - can use. 
13ExpandedBlockEnd.gif */

14 None.gif 
15 None.gif namespace  Mediator_DesignPattern
16 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
17InBlock.gif    using System;
18InBlock.gif
19InBlock.gif    class Mediator 
20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
21InBlock.gif        private DataProviderColleague dataProvider;
22InBlock.gif        private DataConsumerColleague dataConsumer;
23InBlock.gif        public void IntroduceColleagues(DataProviderColleague c1, DataConsumerColleague c2)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            dataProvider = c1;
26InBlock.gif            dataConsumer = c2;            
27ExpandedSubBlockEnd.gif        }

28InBlock.gif        
29InBlock.gif        public void DataChanged()
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            int i = dataProvider.MyData;
32InBlock.gif            dataConsumer.NewValue(i);
33ExpandedSubBlockEnd.gif        }

34ExpandedSubBlockEnd.gif    }

35InBlock.gif
36InBlock.gif    class DataConsumerColleague 
37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
38InBlock.gif        public void NewValue(int i)
39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
40InBlock.gif            Console.WriteLine("New value {0}", i);
41ExpandedSubBlockEnd.gif        }

42ExpandedSubBlockEnd.gif    }

43InBlock.gif
44InBlock.gif    class DataProviderColleague
45ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
46InBlock.gif        private Mediator mediator;
47InBlock.gif        private int iMyData=0;
48InBlock.gif        public int MyData 
49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
50InBlock.gif            get 
51ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
52InBlock.gif                return iMyData;
53ExpandedSubBlockEnd.gif            }

54InBlock.gif            set 
55ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
56InBlock.gif                iMyData = value;
57ExpandedSubBlockEnd.gif            }

58ExpandedSubBlockEnd.gif        }

59InBlock.gif        public DataProviderColleague(Mediator m)
60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
61InBlock.gif            mediator = m;
62ExpandedSubBlockEnd.gif        }

63InBlock.gif
64InBlock.gif        public void ChangeData()
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            iMyData = 403;
67InBlock.gif
68InBlock.gif            // Inform mediator that I have changed the data
69InBlock.gif            if (mediator != null)
70InBlock.gif                mediator.DataChanged();    
71ExpandedSubBlockEnd.gif        }
        
72ExpandedSubBlockEnd.gif    }

73InBlock.gif
74ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
75InBlock.gif    ///    Summary description for Client.
76ExpandedSubBlockEnd.gif    /// </summary>

77InBlock.gif    public class Client
78ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
79InBlock.gif        public static int Main(string[] args)
80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
81InBlock.gif            Mediator m = new Mediator();
82InBlock.gif            DataProviderColleague c1 = new DataProviderColleague(m);
83InBlock.gif            DataConsumerColleague c2 = new DataConsumerColleague();
84InBlock.gif            m.IntroduceColleagues(c1,c2);
85InBlock.gif
86InBlock.gif            c1.ChangeData();
87InBlock.gif
88InBlock.gif            return 0;
89ExpandedSubBlockEnd.gif        }

90ExpandedSubBlockEnd.gif    }

91ExpandedBlockEnd.gif}

92 None.gif
93 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/08/09/210468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值