在用户与用户直接聊天的设计方案中,用户对象之间存在很强的关联性,将导致系统出现如下问题:
参与者:
Mediator(中介者):中介者定义一个接口用于与各同事对象通信。
ConcreteMediator(具体中介者):具体中介者通过协调各同事对象实现协作行为。了解并维护它的各个同事。
Colleague Class(参与者类):每一个同事者类都知道他的中介者对象,每一个同事对象在需要与其他的同事者通信的时候,与他的中介者通信。
代码一、
public abstract class Mediator { protected ArrayList colleagues; public void register(Colleague colleague) { colleagues.add(colleague); } public abstract void operation(); } public abstract class Colleague { protected Mediator mediator; public Colleague(Mediator mediator) { this.mediator=mediator; } public abstract void method1(); public abstract void method2(); } public class ConcreteMediator extends Mediator { public void operation() { ...... ((Colleague)(colleagues.get(0))).method1(); ...... } } public class ConcreteColleague extends Colleague { public ConcreteColleague(Mediator mediator) { super(mediator); } public void method1() { ...... } public void method2() { mediator.operation1(); } }
代码二、
Mediator(中介者)
public abstract class AbstractChatroom { public abstract void register(Member member); public abstract void sendText(String from,String to,String message); public abstract void sendImage(String from,String to,String message); }
ConcreteMediator(具体中介者)
import java.util.*; public class ChatGroup extends AbstractChatroom { private Hashtable members=new Hashtable(); public void register(Member member) { if(!members.contains(member)) { members.put(member.getName(),member); member.setChatroom(this); } } public void sendText(String from,String to,String message) { Member member=(Member)members.get(to); String newMessage=message; newMessage=message.replaceAll("日","*"); member.receiveText(from,newMessage); } public void sendImage(String from,String to,String image) { Member member=(Member)members.get(to); //模拟图片大小判断 if(image.length()>5) { System.out.println("图片太大,发送失败!"); } else { member.receiveImage(from,image); } } }
Colleague Class(参与者类)
public abstract class Member { protected AbstractChatroom chatroom; protected String name; public Member(String name) { this.name=name; } public String getName() { return name; } public void setName(String name) { this.name=name; } public AbstractChatroom getChatroom() { return chatroom; } public void setChatroom(AbstractChatroom chatroom) { this.chatroom=chatroom; } public abstract void sendText(String to,String message); public abstract void sendImage(String to,String image); public void receiveText(String from,String message) { System.out.println(from + "发送文本给" + this.name + ",内容为:" + message); } public void receiveImage(String from,String image) { System.out.println(from + "发送图片给" + this.name + ",内容为:" + image); } }
public class DiamondMember extends Member { public DiamondMember(String name) { super(name); } public void sendText(String to,String message) { System.out.println("钻石会员发送信息:"); chatroom.sendText(name,to,message); //发送 } public void sendImage(String to,String image) { System.out.println("钻石会员发送图片:"); chatroom.sendImage(name,to,image); //发送 } }
public class CommonMember extends Member { public CommonMember(String name) { super(name); } public void sendText(String to,String message) { System.out.println("普通会员发送信息:"); chatroom.sendText(name,to,message); //发送 } public void sendImage(String to,String image) { System.out.println("普通会员不能发送图片!"); } }测试
public class Client { public static void main(String args[]) { AbstractChatroom happyChat=new ChatGroup(); Member member1,member2,member3,member4,member5; member1=new DiamondMember("张三"); member2=new DiamondMember("李四"); member3=new CommonMember("王五"); member4=new CommonMember("小芳"); member5=new CommonMember("小红"); happyChat.register(member1); happyChat.register(member2); happyChat.register(member3); happyChat.register(member4); happyChat.register(member5); member1.sendText("李四","李四,你好!"); member2.sendText("张三","张三,你好!"); member1.sendText("李四","今天天气不错,有日!"); member2.sendImage("张三","一个很大很大的太阳"); member2.sendImage("张三","太阳"); member3.sendText("小芳","还有问题吗?"); member3.sendText("小红","还有问题吗?"); member4.sendText("王五","没有了,谢谢!"); member5.sendText("王五","我也没有了!"); member5.sendImage("王五","谢谢"); } }
中介者模式可以使对象之间的关系数量急剧减少
中介者承担两方面的职责:
中介者模式的缺点
java.util.Timer
(all scheduleXXX()
methods)java.util.concurrent.Executor#execute()
java.util.concurrent.ExecutorService
(the invokeXXX()
and submit()
methods)java.util.concurrent.ScheduledExecutorService
(all scheduleXXX()
methods)java.lang.reflect.Method#invoke()
中介者模式与GUI开发
中介者模式可以方便地应用于图形界面(GUI)开发中,在比较复杂的界面中可能存在多个界面组件之间的交互关系。
对于这些复杂的交互关系,有时候我们可以引入一个中介者类,将这些交互的组件作为具体的同事类,将它们之间的引用和控制关系交由中介者负责,在一定程度上简化系统的交互,这也是中介者模式的常见应用之一。