补11.28
1.类图
┌─────────────────┐
│ AbstractChatRoom │
├─────────────────┤
│ +register() │
│ +sendText() │
│ +sendImage() │
└─────────────────┘
△
│
┌───────────┐
│CommonChatRoom│
│StrictChatRoom│
└───────────┘
△
│
┌─────────────────┐
│ Member │
├─────────────────┤
│ +sendText() │
│ +sendImage() │
└─────────────────┘
△
│
┌───────────┐
│ DiamondMember │
└───────────┘
2.源代码
import java.text.SimpleDateFormat;
import java.util.*;
/**
- 虚拟聊天室系统 - 中介者模式实现
- 包含普通聊天室、严格聊天室、普通会员和钻石会员
*/
// 抽象中介者 - 聊天室
abstract class AbstractChatRoom {
protected Map<String, Member> members = new HashMap<>();
// 注册会员
public void register(Member member) {if (!members.containsKey(member.getName())) {members.put(member.getName(), member);member.setChatRoom(this);System.out.println("【系统】" + member.getName() + " 加入聊天室");}
}// 发送文本消息
public abstract void sendText(String from, String to, String message);// 发送图片消息
public abstract void sendImage(String from, String to, String imageName, int imageSize);// 广播文本消息
public abstract void broadcastText(String from, String message);// 广播图片消息
public abstract void broadcastImage(String from, String imageName, int imageSize);// 获取在线会员列表
public List<String> getOnlineMembers() {return new ArrayList<>(members.keySet());
}
}
// 普通聊天室
class CommonChatRoom extends AbstractChatRoom {
@Override
public void sendText(String from, String to, String message) {
Member member = members.get(to);
if (member != null) {
System.out.println("【私聊】" + from + " -> " + to + ": " + message);
member.receiveText(from, message);
} else {
System.out.println("【系统】用户 " + to + " 不存在!");
}
}
@Override
public void sendImage(String from, String to, String imageName, int imageSize) {Member member = members.get(to);if (member != null) {System.out.println("【图片】" + from + " -> " + to + ": " + imageName + " (" + imageSize + "M)");member.receiveImage(from, imageName, imageSize);} else {System.out.println("【系统】用户 " + to + " 不存在!");}
}@Override
public void broadcastText(String from, String message) {System.out.println("【广播】" + from + ": " + message);for (Member member : members.values()) {if (!member.getName().equals(from)) {member.receiveText(from, message);}}
}@Override
public void broadcastImage(String from, String imageName, int imageSize) {System.out.println("【广播图片】" + from + ": " + imageName + " (" + imageSize + "M)");for (Member member : members.values()) {if (!member.getName().equals(from)) {member.receiveImage(from, imageName, imageSize);}}
}
}
// 严格聊天室 - 新的具体聊天室类
class StrictChatRoom extends AbstractChatRoom {
private static final int MAX_IMAGE_SIZE = 20; // 图片大小限制20M
private static final int MAX_TEXT_LENGTH = 100; // 文本长度限制100字符
@Override
public void sendText(String from, String to, String message) {// 检查文本长度if (message.length() > MAX_TEXT_LENGTH) {System.out.println("【系统错误】文本长度不能超过 " + MAX_TEXT_LENGTH + " 个字符");return;}Member member = members.get(to);if (member != null) {System.out.println("【严格私聊】" + from + " -> " + to + ": " + message);member.receiveText(from, message);} else {System.out.println("【系统】用户 " + to + " 不存在!");}
}@Override
public void sendImage(String from, String to, String imageName, int imageSize) {// 检查图片大小if (imageSize > MAX_IMAGE_SIZE) {System.out.println("【系统错误】图片大小不能超过 " + MAX_IMAGE_SIZE + "M");return;}Member member = members.get(to);if (member != null) {System.out.println("【严格图片】" + from + " -> " + to + ": " + imageName + " (" + imageSize + "M)");member.receiveImage(from, imageName, imageSize);} else {System.out.println("【系统】用户 " + to + " 不存在!");}
}@Override
public void broadcastText(String from, String message) {// 检查文本长度if (message.length() > MAX_TEXT_LENGTH) {System.out.println("【系统错误】文本长度不能超过 " + MAX_TEXT_LENGTH + " 个字符");return;}System.out.println("【严格广播】" + from + ": " + message);for (Member member : members.values()) {if (!member.getName().equals(from)) {member.receiveText(from, message);}}
}@Override
public void broadcastImage(String from, String imageName, int imageSize) {// 检查图片大小if (imageSize > MAX_IMAGE_SIZE) {System.out.println("【系统错误】图片大小不能超过 " + MAX_IMAGE_SIZE + "M");return;}System.out.println("【严格广播图片】" + from + ": " + imageName + " (" + imageSize + "M)");for (Member member : members.values()) {if (!member.getName().equals(from)) {member.receiveImage(from, imageName, imageSize);}}
}
}
// 抽象同事类 - 会员
abstract class Member {
protected String name;
protected AbstractChatRoom chatRoom;
public Member(String name) {this.name = name;
}public String getName() {return name;
}public void setChatRoom(AbstractChatRoom chatRoom) {this.chatRoom = chatRoom;
}// 发送文本消息
public abstract void sendText(String to, String message);// 发送图片消息
public abstract void sendImage(String to, String imageName, int imageSize);// 接收文本消息
public abstract void receiveText(String from, String message);// 接收图片消息
public abstract void receiveImage(String from, String imageName, int imageSize);// 广播文本消息
public void broadcastText(String message) {if (chatRoom != null) {chatRoom.broadcastText(name, message);}
}// 广播图片消息
public void broadcastImage(String imageName, int imageSize) {if (chatRoom != null) {chatRoom.broadcastImage(name, imageName, imageSize);}
}
}
// 普通会员
class CommonMember extends Member {
public CommonMember(String name) {
super(name);
}
@Override
public void sendText(String to, String message) {if (chatRoom != null) {chatRoom.sendText(name, to, message);}
}@Override
public void sendImage(String to, String imageName, int imageSize) {if (chatRoom != null) {chatRoom.sendImage(name, to, imageName, imageSize);}
}@Override
public void receiveText(String from, String message) {System.out.println("【接收】" + name + " 收到来自 " + from + " 的消息: " + message);
}@Override
public void receiveImage(String from, String imageName, int imageSize) {System.out.println("【接收图片】" + name + " 收到来自 " + from + " 的图片: " + imageName + " (" + imageSize + "M)");
}
}
// 钻石会员 - 新的具体会员类
class DiamondMember extends Member {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public DiamondMember(String name) {super(name);
}@Override
public void sendText(String to, String message) {if (chatRoom != null) {// 在信息后加上时间String messageWithTime = message + " [发送时间: " + dateFormat.format(new Date()) + "]";chatRoom.sendText(name, to, messageWithTime);}
}@Override
public void sendImage(String to, String imageName, int imageSize) {if (chatRoom != null) {chatRoom.sendImage(name, to, imageName, imageSize);}
}@Override
public void receiveText(String from, String message) {System.out.println("【钻石接收】" + name + " 收到来自 " + from + " 的消息: " + message);
}@Override
public void receiveImage(String from, String imageName, int imageSize) {System.out.println("【钻石接收图片】" + name + " 收到来自 " + from + " 的图片: " + imageName + " (" + imageSize + "M) [接收时间: " + dateFormat.format(new Date()) + "]");
}// 钻石会员特有方法 - 发送带时间的广播
public void broadcastTextWithTime(String message) {String messageWithTime = message + " [广播时间: " + dateFormat.format(new Date()) + "]";broadcastText(messageWithTime);
}
}
// 客户端测试类
public class ChatRoomSystem {
public static void main(String[] args) {
System.out.println("=== 虚拟聊天室系统测试 ===\n");
// 创建聊天室AbstractChatRoom commonRoom = new CommonChatRoom();AbstractChatRoom strictRoom = new StrictChatRoom();// 创建会员Member member1 = new CommonMember("张三");Member member2 = new CommonMember("李四");Member member3 = new DiamondMember("王五");Member member4 = new DiamondMember("赵六");System.out.println("\n=== 测试普通聊天室 ===");// 注册到普通聊天室commonRoom.register(member1);commonRoom.register(member2);commonRoom.register(member3);// 测试普通聊天室功能member1.sendText("李四", "你好,李四!这是一条普通消息。");member2.sendImage("张三", "风景.jpg", 15);member3.broadcastText("大家好,我是钻石会员王五!");System.out.println("\n=== 测试严格聊天室 ===");// 注册到严格聊天室strictRoom.register(member3);strictRoom.register(member4);// 测试严格聊天室限制功能member3.sendText("赵六", "这是一条正常长度的文本消息。");member4.sendText("王五", "这是一条非常长的文本消息,这条消息的长度已经超过了100个字符的限制,应该会被系统拒绝发送。这条消息确实太长了!");member3.sendImage("赵六", "小图片.png", 10);member4.sendImage("王五", "大图片.jpg", 25);System.out.println("\n=== 测试钻石会员特色功能 ===");// 测试钻石会员的时间戳功能DiamondMember diamondMember = (DiamondMember) member3;diamondMember.sendText("赵六", "这是带时间戳的私聊消息");diamondMember.broadcastTextWithTime("这是带时间戳的广播消息");System.out.println("\n=== 测试在线会员列表 ===");System.out.println("普通聊天室在线会员: " + commonRoom.getOnlineMembers());System.out.println("严格聊天室在线会员: " + strictRoom.getOnlineMembers());System.out.println("\n=== 测试完成 ===");
}