目的

集中相关对象之间复杂的沟通和控制方式。

类图

  • Mediator: 中介者,定义一个接口用于与各同事(Colleague)对象通信。
  • Colleague: 同事,相关对象

中介者类图.png

实现

Alarm(闹钟)、CoffeePot(咖啡壶)、Calendar(日历)、Sprinkler(喷头)是一组相关的对象,在某个对象的事件产生时需要去操作其他对象,形成了下面这种依赖结构:
中介者1.jpg
使用中介者模式可以将复杂的依赖结构变成星形结构。
中介者2.jpg

  1. public abstract class Colleague {
  2. public abstract void onEvent(Mediator mediator);
  3. }
  4. public class Alarm extends Colleague {
  5. @Override
  6. public void onEvent(Mediator mediator) {
  7. mediator.doEvent("alarm");
  8. }
  9. public void doAlarm() {
  10. System.out.println("doAlarm()");
  11. }
  12. }
  13. public class CoffeePot extends Colleague {
  14. @Override
  15. public void onEvent(Mediator mediator) {
  16. mediator.doEvent("coffeePot");
  17. }
  18. public void doCoffeePot() {
  19. System.out.println("doCoffeePot()");
  20. }
  21. }
  22. public class Calender extends Colleague {
  23. @Override
  24. public void onEvent(Mediator mediator) {
  25. mediator.doEvent("calender");
  26. }
  27. public void doCalender() {
  28. System.out.println("doCalender()");
  29. }
  30. }
  31. public class Sprinkler extends Colleague {
  32. @Override
  33. public void onEvent(Mediator mediator) {
  34. mediator.doEvent("sprinkler");
  35. }
  36. public void doSprinkler() {
  37. System.out.println("doSprinkler()");
  38. }
  39. }
  40. public abstract class Mediator {
  41. public abstract void doEvent(String eventType);
  42. }
  43. public class ConcreteMediator extends Mediator {
  44. private Alarm alarm;
  45. private CoffeePot coffeePot;
  46. private Calender calender;
  47. private Sprinkler sprinkler;
  48. public ConcreteMediator(Alarm alarm, CoffeePot coffeePot, Calender calender, Sprinkler sprinkler) {
  49. this.alarm = alarm;
  50. this.coffeePot = coffeePot;
  51. this.calender = calender;
  52. this.sprinkler = sprinkler;
  53. }
  54. @Override
  55. public void doEvent(String eventType) {
  56. switch (eventType) {
  57. case "alarm":
  58. doAlarmEvent();
  59. break;
  60. case "coffeePot":
  61. doCoffeePotEvent();
  62. break;
  63. case "calender":
  64. doCalenderEvent();
  65. break;
  66. default:
  67. doSprinklerEvent();
  68. }
  69. }
  70. public void doAlarmEvent() {
  71. alarm.doAlarm();
  72. coffeePot.doCoffeePot();
  73. calender.doCalender();
  74. sprinkler.doSprinkler();
  75. }
  76. public void doCoffeePotEvent() {
  77. // ...
  78. }
  79. public void doCalenderEvent() {
  80. // ...
  81. }
  82. public void doSprinklerEvent() {
  83. // ...
  84. }
  85. }
  86. public class Client {
  87. public static void main(String[] args) {
  88. Alarm alarm = new Alarm();
  89. CoffeePot coffeePot = new CoffeePot();
  90. Calender calender = new Calender();
  91. Sprinkler sprinkler = new Sprinkler();
  92. Mediator mediator = new ConcreteMediator(alarm, coffeePot, calender, sprinkler);
  93. // 闹钟事件到达,调用中介者就可以操作相关对象
  94. alarm.onEvent(mediator);
  95. }
  96. }
  97. 运行结果
  98. doAlarm()
  99. doCoffeePot()
  100. doCalender()
  101. doSprinkler()

JDK中的体现