Design Pattern - Mediator(Java)

青旅半醒 2022-05-19 02:21 343阅读 0赞

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

Definition

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

Participants

  1. The classes and/or objects participating in this pattern are:
  • Mediator (IChatroom)

    • Defines an interface for communicating with Colleague objects
  • ConcreteMediator (Chatroom)

    • Implements cooperative behavior by coordinating Colleague objects
    • Knows and maintains its colleagues
  • Colleague classes (Participant)

    • Each Colleague class knows its Mediator object
    • Each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague

Sample Code in Java


This structural code demonstrates the Mediator pattern facilitating loosely coupled communication between different objects and object types. The mediator is a central hub through which all interaction must take place.

  1. package chimomo.learning.java.designpattern.mediator.sample;
  2. /**
  3. * The 'Colleague' class
  4. *
  5. * @author Chimomo
  6. */
  7. class Colleague {
  8. // The mediator
  9. protected Mediator mediator;
  10. /**
  11. * Initializes a new instance of the "Colleague" class
  12. *
  13. * @param mediator The mediator
  14. */
  15. Colleague(Mediator mediator) {
  16. this.mediator = mediator;
  17. }
  18. }
  19. package chimomo.learning.java.designpattern.mediator.sample;
  20. /**
  21. * A 'ConcreteColleague' class
  22. *
  23. * @author Chimomo
  24. */
  25. class ConcreteColleague1 extends Colleague {
  26. /**
  27. * Initializes a new instance of the "ConcreteColleague1" class
  28. *
  29. * @param mediator The mediator
  30. */
  31. ConcreteColleague1(Mediator mediator) {
  32. super(mediator);
  33. }
  34. /**
  35. * Notify
  36. *
  37. * @param message The message
  38. */
  39. void notify(String message) {
  40. System.out.println("Colleague1 gets message: " + message);
  41. }
  42. /**
  43. * Send
  44. *
  45. * @param message The send
  46. */
  47. public void send(String message) {
  48. this.mediator.send(message, this);
  49. }
  50. }
  51. package chimomo.learning.java.designpattern.mediator.sample;
  52. /**
  53. * A 'ConcreteColleague' class
  54. *
  55. * @author Chimomo
  56. */
  57. class ConcreteColleague2 extends Colleague {
  58. /**
  59. * Initializes a new instance of the "ConcreteColleague1" class
  60. *
  61. * @param mediator The mediator
  62. */
  63. ConcreteColleague2(Mediator mediator) {
  64. super(mediator);
  65. }
  66. /**
  67. * Notify
  68. *
  69. * @param message The message
  70. */
  71. void notify(String message) {
  72. System.out.println("Colleague2 gets message: " + message);
  73. }
  74. /**
  75. * Send
  76. *
  77. * @param message The message
  78. */
  79. public void send(String message) {
  80. this.mediator.send(message, this);
  81. }
  82. }
  83. package chimomo.learning.java.designpattern.mediator.sample;
  84. /**
  85. * The 'ConcreteMediator' class
  86. *
  87. * @author Chimomo
  88. */
  89. class ConcreteMediator extends Mediator {
  90. // The colleague1
  91. private ConcreteColleague1 colleague1;
  92. // The colleague2
  93. private ConcreteColleague2 colleague2;
  94. /**
  95. * Set concrete colleague1
  96. *
  97. * @param colleague1 The colleague1
  98. */
  99. void setConcreteColleague1(ConcreteColleague1 colleague1) {
  100. this.colleague1 = colleague1;
  101. }
  102. /**
  103. * Set concrete colleague2
  104. *
  105. * @param colleague2 The colleague2
  106. */
  107. void setConcreteColleague2(ConcreteColleague2 colleague2) {
  108. this.colleague2 = colleague2;
  109. }
  110. /**
  111. * Send
  112. *
  113. * @param message The message
  114. * @param colleague The colleague
  115. */
  116. @Override
  117. public void send(String message, Colleague colleague) {
  118. if (colleague == this.colleague1) {
  119. this.colleague2.notify(message);
  120. } else {
  121. this.colleague1.notify(message);
  122. }
  123. }
  124. }
  125. package chimomo.learning.java.designpattern.mediator.sample;
  126. /**
  127. * The 'Mediator' abstract class
  128. *
  129. * @author Chimomo
  130. */
  131. abstract class Mediator {
  132. /**
  133. * Send
  134. *
  135. * @param message The message
  136. * @param colleague The colleague
  137. */
  138. public abstract void send(String message, Colleague colleague);
  139. }
  140. package chimomo.learning.java.designpattern.mediator.sample;
  141. /**
  142. * Startup class for Structural Mediator Design Pattern
  143. *
  144. * @author Chimomo
  145. */
  146. class Program {
  147. /**
  148. * Entry point into console application
  149. *
  150. * @param args The arguments
  151. */
  152. public static void main(String[] args) {
  153. ConcreteMediator m = new ConcreteMediator();
  154. ConcreteColleague1 c1 = new ConcreteColleague1(m);
  155. ConcreteColleague2 c2 = new ConcreteColleague2(m);
  156. m.setConcreteColleague1(c1);
  157. m.setConcreteColleague2(c2);
  158. c1.send("How are you?");
  159. c2.send("Fine, thank you");
  160. }
  161. }
  162. /* ------ Running Results ------
  163. Colleague2 gets message: How are you?
  164. Colleague1 gets message: Fine, thank you
  165. */

This real-world code demonstrates the Mediator pattern facilitating loosely coupled communication between different Participants registering with a Chat room. The Chatroom is the central hub through which all communication takes place. At this point only one-to-one communication is implemented in the Chatroom, but would be trivial to change to one-to-many.

  1. package chimomo.learning.java.designpattern.mediator.realworld;
  2. /**
  3. * The 'Mediator' abstract class
  4. *
  5. * @author Chimomo
  6. */
  7. abstract class AbstractChatRoom {
  8. /**
  9. * Register participant
  10. *
  11. * @param participant The participant
  12. */
  13. public abstract void register(Participant participant);
  14. /**
  15. * Send message
  16. *
  17. * @param from The from
  18. * @param to The to
  19. * @param message The message
  20. */
  21. public abstract void send(String from, String to, String message);
  22. }
  23. package chimomo.learning.java.designpattern.mediator.realworld;
  24. /**
  25. * A 'ConcreteColleague' class
  26. *
  27. * @author Chimomo
  28. */
  29. class Beatle extends Participant {
  30. /**
  31. * Initializes a new instance of the "Beatle" class
  32. *
  33. * @param name The name
  34. */
  35. Beatle(String name) {
  36. super(name);
  37. }
  38. /**
  39. * Receive
  40. *
  41. * @param from The from
  42. * @param message The message
  43. */
  44. public void receive(String from, String message) {
  45. System.out.print("To a Beatle: ");
  46. super.receive(from, message);
  47. }
  48. }
  49. package chimomo.learning.java.designpattern.mediator.realworld;
  50. import java.util.HashMap;
  51. import java.util.Map;
  52. /**
  53. * The 'ConcreteMediator' class
  54. *
  55. * @author Chimomo
  56. */
  57. class ChatRoom extends AbstractChatRoom {
  58. // The participants
  59. private Map<String, Participant> participants = new HashMap<>();
  60. /**
  61. * Register participant
  62. *
  63. * @param participant The participant
  64. */
  65. @Override
  66. public void register(Participant participant) {
  67. if (!this.participants.containsValue(participant)) {
  68. this.participants.put(participant.getName(), participant);
  69. }
  70. participant.setChatRoom(this);
  71. }
  72. /**
  73. * Send message
  74. *
  75. * @param from The from
  76. * @param to The to
  77. * @param message The message
  78. */
  79. @Override
  80. public void send(String from, String to, String message) {
  81. Participant participant = this.participants.get(to);
  82. if (participant != null) {
  83. participant.receive(from, message);
  84. }
  85. }
  86. }
  87. package chimomo.learning.java.designpattern.mediator.realworld;
  88. /**
  89. * A 'ConcreteColleague' class
  90. *
  91. * @author Chimomo
  92. */
  93. class NonBeatle extends Participant {
  94. /**
  95. * Initializes a new instance of the "NonBeatle" class
  96. *
  97. * @param name The name
  98. */
  99. NonBeatle(String name) {
  100. super(name);
  101. }
  102. /**
  103. * Receive
  104. *
  105. * @param from The from
  106. * @param message The message
  107. */
  108. public void receive(String from, String message) {
  109. System.out.print("To a NonBeatle: ");
  110. super.receive(from, message);
  111. }
  112. }
  113. package chimomo.learning.java.designpattern.mediator.realworld;
  114. /**
  115. * The 'AbstractColleague' class
  116. *
  117. * @author Chimomo
  118. */
  119. class Participant {
  120. // The chat room
  121. private ChatRoom chatRoom;
  122. // The participant name
  123. private String name;
  124. /**
  125. * Initializes a new instance of the "Participant" class
  126. *
  127. * @param name The name
  128. */
  129. Participant(String name) {
  130. this.name = name;
  131. }
  132. /**
  133. * Set chat room
  134. *
  135. * @param chatRoom The chat room
  136. */
  137. void setChatRoom(ChatRoom chatRoom) {
  138. this.chatRoom = chatRoom;
  139. }
  140. /**
  141. * Get name
  142. *
  143. * @return The name
  144. */
  145. public String getName() {
  146. return this.name;
  147. }
  148. /**
  149. * Set name
  150. *
  151. * @param name The name
  152. */
  153. public void setName(String name) {
  154. this.name = name;
  155. }
  156. /**
  157. * Receive
  158. *
  159. * @param from The from
  160. * @param message The message
  161. */
  162. public void receive(String from, String message) {
  163. System.out.println(String.format("%s to %s: '%s'", from, this.name, message));
  164. }
  165. /**
  166. * Send
  167. *
  168. * @param to The to
  169. * @param message The message
  170. */
  171. public void send(String to, String message) {
  172. this.chatRoom.send(this.name, to, message);
  173. }
  174. }
  175. package chimomo.learning.java.designpattern.mediator.realworld;
  176. /**
  177. * Startup class for Real-World Mediator Design Pattern
  178. *
  179. * @author Chimomo
  180. */
  181. class Program {
  182. /**
  183. * Entry point into console application
  184. *
  185. * @param args The arguments
  186. */
  187. public static void main(String[] args) {
  188. // Create chat room
  189. ChatRoom chatRoom = new ChatRoom();
  190. // Create participants and register them
  191. Participant george = new Beatle("George");
  192. Participant paul = new Beatle("Paul");
  193. Participant ringo = new Beatle("Ringo");
  194. Participant john = new Beatle("John");
  195. Participant yoko = new NonBeatle("Yoko");
  196. chatRoom.register(george);
  197. chatRoom.register(paul);
  198. chatRoom.register(ringo);
  199. chatRoom.register(john);
  200. chatRoom.register(yoko);
  201. // Chatting participants
  202. yoko.send("John", "Hi John!");
  203. paul.send("Ringo", "All you need is love");
  204. ringo.send("George", "My sweet lord");
  205. paul.send("John", "Can't buy me love");
  206. john.send("Yoko", "My sweet love");
  207. }
  208. }
  209. /* ------ Running Results ------
  210. To a Beatle: Yoko to John: 'Hi John!'
  211. To a Beatle: Paul to Ringo: 'All you need is love'
  212. To a Beatle: Ringo to George: 'My sweet lord'
  213. To a Beatle: Paul to John: 'Can't buy me love'
  214. To a NonBeatle: John to Yoko: 'My sweet love'
  215. */

发表评论

表情:
评论列表 (有 0 条评论,343人围观)

还没有评论,来说两句吧...

相关阅读