标签:String mediator 19 void 中介 软件设计 image message public
实验19:中介者模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解中介者模式的动机,掌握该模式的结构;
2、能够利用中介者模式解决实际问题。
[实验任务一]:虚拟聊天室
在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:
1. 新的具体聊天室中发送的图片大小不得超过20M。
2. 新的具体聊天室中发送的文字长度不得超过100个字符。
3. 新的具体会员类可以发送图片信息和文本信息。
4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。
修改客户端测试类,注意原有系统类代码和客户端代码的修改。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
3. 注意编程规范。
1.
2.
// 抽象中介者类
abstract class Mediator {
public abstract void send(String message, Client client);
public abstract void sendImage(String image, Client client);
}
// 具体中介者类
class ChatRoomMediator extends Mediator {
private List<Client> clients = new ArrayList<>();
public void addClient(Client client) {
clients.add(client);
}
@Override
public void send(String message, Client client) {
for (Client c : clients) {
if (c != client) {
c.receive(message);
}
}
}
@Override
public void sendImage(String image, Client client) {
for (Client c : clients) {
if (c != client) {
c.receiveImage(image);
}
}
}
}
// 抽象客户端类
abstract class Client {
protected Mediator mediator;
protected String name;
public Client(Mediator mediator, String name) {
this.mediator = mediator;
this.name = name;
}
public abstract void sendMessage(String message);
public abstract void sendImage(String image);
public abstract void receive(String message);
public abstract void receiveImage(String image);
}
// 具体客户端类
class ConcreteChatRoom extends Client {
public ConcreteChatRoom(Mediator mediator, String name) {
super(mediator, name);
}
@Override
public void sendMessage(String message) {
if (message.length() <= 100) {
mediator.send(message, this);
}
}
@Override
public void sendImage(String image) {
if (image.getBytes().length <= 20 * 1024 * 1024) {
mediator.sendImage(image, this);
}
}
@Override
public void receive(String message) {
System.out.println(name + " received message: " + message);
}
@Override
public void receiveImage(String image) {
System.out.println(name + " received image.");
}
}
// 新的具体客户端类
class NewClient extends Client {
public NewClient(Mediator mediator, String name) {
super(mediator, name);
}
@Override
public void sendMessage(String message) {
if (message.length() <= 100) {
mediator.send(message + " [" + new Date() + "]", this);
}
}
@Override
public void sendImage(String image) {
if (image.getBytes().length <= 20 * 1024 * 1024) {
mediator.sendImage(image, this);
}
}
@Override
public void receive(String message) {
System.out.println(name + " received message: " + message);
}
@Override
public void receiveImage(String image) {
System.out.println(name + " received image.");
}
}
// 客户端测试类
public class ChatRoomTest {
public static void main(String[] args) {
ChatRoomMediator mediator = new ChatRoomMediator();
ConcreteChatRoom chatRoom1 = new ConcreteChatRoom(mediator, "ChatRoom1");
ConcreteChatRoom chatRoom2 = new ConcreteChatRoom(mediator, "ChatRoom2");
NewClient newClient = new NewClient(mediator, "NewClient");
mediator.addClient(chatRoom1);
mediator.addClient(chatRoom2);
mediator.addClient(newClient);
chatRoom1.sendMessage("Hello, World!");
newClient.sendMessage("Hello, New World!");
chatRoom1.sendImage("image1.jpg");
newClient.sendImage("image2.jpg");
}
}
标签:String,
mediator,
19,
void,
中介,
软件设计,
image,
message,
public
From: https://www.cnblogs.com/xuan-2004/p/18575186