中介者模式
定义
在多对象交互业务中,引入统一业务处理的第三方,实现对象间的解耦。
类图
类图中包含者未引入中介者模式的方法,详情请看代码
代码
1 /// <summary> 2 /// 房子管理者 3 /// </summary> 4 public abstract class HouseManger 5 { 6 protected int moneyCount = 0; 7 public string Name { get; set; } 8 public HouseManger(string name, int moneyCount) 9 { 10 this.moneyCount = moneyCount; 11 this.Name = name; 12 } 13 public abstract void Deal(int money, bool flag); 14 } 15 /// <summary> 16 /// A类管理者 17 /// </summary> 18 public class HouseManagerA : HouseManger 19 { 20 public HouseManagerA(string name, int money) 21 : base(name, money) 22 { 23 24 } 25 public override void Deal(int money, bool flag) 26 { 27 moneyCount = flag ? moneyCount + money : moneyCount - money; 28 } 29 #region 不走中介的模式,增加了对HouseManagerB的强引用 30 /// <summary> 31 /// 卖房子 32 /// </summary> 33 /// <param name="money"></param> 34 /// <param name="buyer"></param> 35 public void Sell(int money, HouseManagerB buyer) 36 { 37 moneyCount += money; 38 Console.WriteLine("将房产正转置{0}名下", buyer.Name); 39 buyer.Buy(money, this); 40 41 } 42 /// <summary> 43 /// 买房子 44 /// </summary> 45 /// <param name="money"></param> 46 /// <param name="seller"></param> 47 public void Buy(int money, HouseManagerB seller) 48 { 49 moneyCount -= money; 50 Console.WriteLine("买到{0}的房子了", seller.Name); 51 } 52 #endregion 53 } 54 /// <summary> 55 /// B类管理者 56 /// </summary> 57 public class HouseManagerB : HouseManger 58 { 59 public HouseManagerB(string name, int money = 500000) 60 : base(name, money) 61 { 62 63 } 64 public override void Deal(int money, bool flag) 65 { 66 moneyCount = flag ? moneyCount + money : moneyCount - money; 67 } 68 #region 不走中介的模式,增加了对HouseManagerA的强引用,走中介则不会 69 /// <summary> 70 /// 买房子 71 /// </summary> 72 /// <param name="money"></param> 73 /// <param name="seller"></param> 74 public void Buy(int money, HouseManagerA seller) 75 { 76 moneyCount -= money; 77 Console.WriteLine("买到{0}的房子了", seller.Name); 78 } 79 /// <summary> 80 /// 卖房子 81 /// </summary> 82 /// <param name="money"></param> 83 /// <param name="buyer"></param> 84 public void Sell(int money, HouseManagerA buyer) 85 { 86 moneyCount += money; 87 Console.WriteLine("将房产正转置{0}名下", buyer.Name); 88 buyer.Buy(money, this); 89 90 } 91 #endregion 92 93 } 94 public abstract class HouseMediator 95 { 96 protected HouseManagerA a; 97 protected HouseManagerB b; 98 public HouseMediator(HouseManagerA a, HouseManagerB b) 99 { 100 this.a = a; 101 this.b = b; 102 } 103 /// <summary> 104 /// A卖与B 105 /// </summary> 106 public abstract void AToBDeal(int money); 107 /// <summary> 108 /// B卖与A 109 /// </summary> 110 public abstract void BToADeal(int money); 111 } 112 /// <summary> 113 /// 免费的中介者(房产中介是收钱的,相当于引入新业务,此处只是说明中介者模式) 114 /// </summary> 115 public class FreeHouseMediator : HouseMediator 116 { 117 118 public override void AToBDeal(int money) 119 { 120 this.a.Deal(money, true); 121 this.b.Deal(money, false); 122 Console.WriteLine("{0}将房子卖与了{1}", a.Name, b.Name); 123 } 124 125 public override void BToADeal(int money) 126 { 127 this.b.Deal(money, true); 128 this.a.Deal(money, false); 129 Console.WriteLine("{0}将房子卖与了{1}", b.Name, a.Name); 130 } 131 }
总结
中介者模式解耦对象之间的强引用,将通知通信交互的业务抽取到中介中来,实现解耦。现实中有很多例子,例如通过微信群进行同学之间信息的通知,微信群就成了一个消息转发的中介。不过自己觉得反而在实际的编程中用的却不是很多。