示例类图
示例代码
抽象中介者
- 定义了中介者为成员提供的服务;
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 image);
}
抽象成员类
- 在抽象成员类中建立了成员和中介者的关系,成员要知道哪个中介者为我服务;
public abstract class Member {
protected AbstractChatRoom chatRoom;
protected String name;
public Member(String name) {
this.name = name;
}
public AbstractChatRoom getChatRoom() {
return chatRoom;
}
public void setChatRoom(AbstractChatRoom chatRoom) {
this.chatRoom = chatRoom;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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);
}
}
具体中介者
- 成员间的通信都委托给了中介者了;
- 成员注册到中介者中,并且成员也知道哪个中介者为我服务;
import java.util.Hashtable;
public class ChatGroup extends AbstractChatRoom{
private Hashtable<String, Member> members = new Hashtable();
@Override
public void register(Member member) {
if (!members.contains(member)) {
members.put(member.getName(), member);
member.setChatRoom(this);
}
}
@Override
public void sendText(String from, String to, String message) {
Member toMember = members.get(to);
String clearedMessage = message.replaceAll("日", "*");
toMember.receiveImage(from, clearedMessage);
}
@Override
public void sendImage(String from, String to, String image) {
Member toMember = members.get(to);
if (image.length() > 15) {
System.out.println("图片太大,发送失败");
} else {
toMember.receiveImage(from, image);
}
}
}
具体成员类
- 具体成员间的通信委托给了中介者;
public class CommonMember extends Member{
public CommonMember(String name) {
super(name);
}
@Override
public void sendText(String to, String message) {
System.out.println("普通会员发送文本:");
chatRoom.sendText(this.name, to, message);
}
@Override
public void sendImage(String to, String image) {
System.out.println("普通会员不能发送图片!");
}
}
public class VIPMember extends Member {
public VIPMember(String name) {
super(name);
}
@Override
public void sendText(String to, String message) {
System.out.println("VIP会员发送文本:");
chatRoom.sendText(this.name, to, message);
}
@Override
public void sendImage(String to, String image) {
System.out.println("VIP会员发送图片:");
chatRoom.sendImage(this.name, to, image);
}
}
客户端
- 成员注册进中介者;
- 成员间的通信委托给中介者;
public class Test {
public static void main(String[] args) {
AbstractChatRoom chatRoom = new ChatGroup();
Member member1 = new VIPMember("张三");
Member member2 = new VIPMember("李四");
Member member3 = new CommonMember("王五");
Member member4 = new CommonMember("小芳");
Member member5 = new CommonMember("小红");
chatRoom.register(member1);
chatRoom.register(member2);
chatRoom.register(member3);
chatRoom.register(member4);
chatRoom.register(member5);
member1.sendText("李四", "你好");
}
}
输出:
VIP会员发送文本:
张三发送图片给李四,内容为:你好