package Tutorial19; // 中介者接口 public interface Mediator { void sendMessage(AbstractChatRoom chatRoom, AbstractMember member, String message); void sendPicture(AbstractChatRoom chatRoom, AbstractMember member, byte[] picture); }
package Tutorial19; // 抽象聊天室类 public abstract class AbstractChatRoom { protected Mediator mediator; public AbstractChatRoom(Mediator mediator) { this.mediator = mediator; } public abstract void sendMessage(AbstractMember member, String message); public abstract void sendPicture(AbstractMember member, byte[] picture); }
package Tutorial19; // 抽象会员类 public abstract class AbstractMember { protected String name; protected AbstractChatRoom chatRoom; public AbstractMember(String name) { this.name = name; } public void setChatRoom(AbstractChatRoom chatRoom) { this.chatRoom = chatRoom; } public abstract void sendMessage(String message); public abstract void sendPicture(byte[] picture); }
package Tutorial19; // 新的具体聊天室类,限制发送图片大小和文字长度 public class NewChatRoom extends AbstractChatRoom { private static final int MAX_PICTURE_SIZE = 20 * 1024 * 1024; // 20M private static final int MAX_TEXT_LENGTH = 100; public NewChatRoom(Mediator mediator) { super(mediator); } @Override public void sendMessage(AbstractMember member, String message) { if (message.length() <= MAX_TEXT_LENGTH) { mediator.sendMessage(this, member, message); } else { System.out.println("发送的文字长度超过限制,无法发送。"); } } @Override public void sendPicture(AbstractMember member, byte[] picture) { if (picture.length <= MAX_PICTURE_SIZE) { mediator.sendPicture(this, member, picture); } else { System.out.println("发送的图片大小超过限制,无法发送。"); } } }
package Tutorial19; // 新的具体会员类,可以发送图片和文本信息,并在文本信息后添加时间 import java.text.SimpleDateFormat; import java.util.Date; public class NewMember extends AbstractMember { public NewMember(String name) { super(name); } @Override public void sendMessage(String message) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timestampedMessage = message + " [" + sdf.format(new Date()) + "]"; chatRoom.sendMessage(this, timestampedMessage); } @Override public void sendPicture(byte[] picture) { chatRoom.sendPicture(this, picture); } }
package Tutorial19; // 客户端测试类 public class ClientTest { public static void main(String[] args) { // 创建中介者对象 Mediator mediator = new Mediator() { @Override public void sendMessage(AbstractChatRoom chatRoom, AbstractMember member, String message) { System.out.println(member.name + " 在 " + chatRoom.getClass().getSimpleName() + " 发送文本消息:" + message); } @Override public void sendPicture(AbstractChatRoom chatRoom, AbstractMember member, byte[] picture) { System.out.println(member.name + " 在 " + chatRoom.getClass().getSimpleName() + " 发送图片消息"); } }; // 创建新的具体聊天室 NewChatRoom newChatRoom = new NewChatRoom(mediator); // 创建新的具体会员 NewMember member1 = new NewMember("会员1"); NewMember member2 = new NewMember("会员2"); // 设置会员所属的聊天室 member1.setChatRoom(newChatRoom); member2.setChatRoom(newChatRoom); // 会员发送文本消息 member1.sendMessage("这是一条不超过100字符的消息"); member2.sendMessage("我也发一条消息试试"); // 模拟发送图片(这里只是示例,实际应替换为真实的图片字节数组) byte[] samplePicture = new byte[10 * 1024 * 1024]; // 10M大小的示例图片字节数组 member1.sendPicture(samplePicture); } }
标签:String,软件设计,Tutorial19,AbstractMember,public,sendMessage,chatRoom,void From: https://www.cnblogs.com/muzhaodi/p/18551609