在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:
1. 新的具体聊天室中发送的图片大小不得超过20M。
2. 新的具体聊天室中发送的文字长度不得超过100个字符。
3. 新的具体会员类可以发送图片信息和文本信息。
4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。
修改客户端测试类,注意原有系统类代码和客户端代码的修改。
package org.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 image); }
package org.Mediator; import java.util.HashMap; 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); 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); if(image.length()>20){ System.out.println("图片过大,发送失败!"); }else{ member.receiveImage(from,image); } } }
package org.Mediator; public abstract class Member { protected AbstractChatroom chatroom; protected String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Member(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); } }
package org.Mediator; 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); } }
package org.Mediator; public class Client { public static void main(String[] args) { AbstractChatroom chatroom=new ChatGroup(); Member member1,member2,member3,member4,member5; member1=new DiamondMember("笑意"); member2=new DiamondMember("晓贰"); member3=new DiamondMember("消散"); member4=new DiamondMember("小肆"); member5=new DiamondMember("小伍"); chatroom.register(member1); chatroom.register(member2); chatroom.register(member3); chatroom.register(member4); chatroom.register(member5); member1.sendText("晓贰","晓贰,你好"); member2.sendText("笑意","笑意,你好"); member3.sendImage("小肆","好大的太阳"); member4.sendImage("消散","**********************************************************"); member5.sendText("消散","*********************************************************" + "************************************************************************************" + "************************************************************************************" + "************************************************************************************"); } }
标签:name,void,模式,Member,中介,chatroom,public,String From: https://www.cnblogs.com/zhiqi2625/p/16869719.html