中介者模式
实验19:中介者模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解中介者模式的动机,掌握该模式的结构;
2、能够利用中介者模式解决实际问题。
[实验任务]:虚拟聊天室
在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:
1. 新的具体聊天室中发送的图片大小不得超过20M。
2. 新的具体聊天室中发送的文字长度不得超过100个字符。
3. 新的具体会员类可以发送图片信息和文本信息。
4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。
修改客户端测试类,注意原有系统类代码和客户端代码的修改。
源代码
/** * 抽象聊天室 */ 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); } import java.util.Hashtable; public class ChatGroup extends AbstractChatroom { private Hashtable 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 member = (Member) members.get(to); String newMessage = message; //模拟不雅字符过滤 newMessage = message.replaceAll("日", "*"); member.receiveText(from, newMessage); } @Override 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); } } } import java.util.Hashtable; /** * 新的具体聊天室 * @author mendianyu */ public class AdvancedChatGroup extends AbstractChatroom { private Hashtable 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 member = (Member) members.get(to); //消息的文字长度不得超过100 if (message.length() > 100) { System.out.println("消息太长了,发送失败!"); } else { member.receiveText(from, message); } } @Override public void sendImage(String from, String to, String image) { Member member = (Member) members.get(to); //图片大小不得超过20M if (image.length() > 20) { System.out.println("图片太大了,发送失败!"); } else { member.receiveImage(from, image); } } } /** * 抽象用户类 */ 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 CommonMember extends Member { public CommonMember(String name) { super(name); } @Override public void sendText(String to, String message) { System.out.println("普通会员发送信息:"); //发送文本 chatroom.sendText(name, to, message); } @Override public void sendImage(String to, String image) { System.out.println("普通会员不能发送图片!"); } } /** * 钻石会员类 */ public class DiamondMember extends Member { public DiamondMember(String name) { super(name); } @Override public void sendText(String to, String message) { System.out.println("钻石会员发送信息:"); //发送文本 chatroom.sendText(name, to, message); } @Override public void sendImage(String to, String image) { System.out.println("钻石会员发送图片:"); //发送图片 chatroom.sendImage(name, to, image); } } import java.text.SimpleDateFormat; import java.util.Date; /** * 铂金会员 * * @author mendianyu */ public class AdvancedMember extends Member { public AdvancedMember(String name) { super(name); } @Override public void sendText(String to, String message) { System.out.println("铂金会员发送文本:"); //在消息后面加上时间 SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String timestamp = dateFormat.format(new Date()); message += " [" + timestamp + "]"; chatroom.sendText(name, to, message); } @Override public void sendImage(String to, String image) { System.out.println("铂金会员发送图片"); chatroom.sendImage(name, to, image); } } public class Client { public static void main(String[] args) { //实例化一个新的聊天室 AbstractChatroom advancedChatroom = new AdvancedChatGroup(); Member member1, member2, member3, member4, member5; member1 = new CommonMember("张三"); member2 = new DiamondMember("李四"); //实例化新的会员 member3 = new AdvancedMember("王五"); //加入聊天室 advancedChatroom.register(member1); advancedChatroom.register(member2); advancedChatroom.register(member3); member1.sendText("李四", "干嘛呢你俩?"); member1.sendText("王五", "干嘛呢你俩?"); member2.sendText("张三", "我在写作业"); //模拟图片超过20M member2.sendImage("张三", "11111111111111111111111111111"); member3.sendText("张三", "我在打游戏"); member3.sendImage("张三", "五连绝世"); member1.sendText("王五", "别打游戏了,快出来玩"); //文字长度超过100,发送失败 member3.sendText("张三","1. 新的具体聊天室中发送的图片大小不得超过20M。\n" + "2. 新的具体聊天室中发送的文字长度不得超过100个字符。\n" + "3. 新的具体会员类可以发送图片信息和文本信息。\n" + "4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。\n" + "修改客户端测试类,注意原有系统类代码和客户端代码的修改。"); } }