标签:
结构 | |
意图 | 用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。 |
适用性 |
|
1 using System; 2 3 class Mediator 4 { 5 private DataProviderColleague dataProvider; 6 private DataConsumerColleague dataConsumer; 7 public void IntroduceColleagues(DataProviderColleague c1, DataConsumerColleague c2) 8 { 9 dataProvider = c1; 10 dataConsumer = c2; 11 } 12 13 public void DataChanged() 14 { 15 int i = dataProvider.MyData; 16 dataConsumer.NewValue(i); 17 } 18 } 19 20 class DataConsumerColleague 21 { 22 public void NewValue(int i) 23 { 24 Console.WriteLine("New value {0}", i); 25 } 26 } 27 28 class DataProviderColleague 29 { 30 private Mediator mediator; 31 private int iMyData=0; 32 public int MyData 33 { 34 get 35 { 36 return iMyData; 37 } 38 set 39 { 40 iMyData = value; 41 } 42 } 43 public DataProviderColleague(Mediator m) 44 { 45 mediator = m; 46 } 47 48 public void ChangeData() 49 { 50 iMyData = 403; 51 52 // Inform mediator that I have changed the data 53 if (mediator != null) 54 mediator.DataChanged(); 55 } 56 } 57 58 /// <summary> 59 /// Summary description for Client. 60 /// </summary> 61 public class Client 62 { 63 public static int Main(string[] args) 64 { 65 Mediator m = new Mediator(); 66 DataProviderColleague c1 = new DataProviderColleague(m); 67 DataConsumerColleague c2 = new DataConsumerColleague(); 68 m.IntroduceColleagues(c1,c2); 69 70 c1.ChangeData(); 71 72 return 0; 73 } 74 }
标签:
原文地址:http://www.cnblogs.com/ziranquliu/p/4663303.html