中介者模式

中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护

Class Diagram

Implementation

封装消息类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class Message {

/**
* 消息来源
*/
String from;
/**
* 消息内容
*/
String content;
/**
* 消息接收方
*/
String to;
/**
* 消息类型
*/
String type;

public Message(String type) {
this.type = type;
}

public Message setFrom(String from){
this.from = from;
return this;
}

public Message setContent(String content) {
this.content = content;
return this;
}

public Message setTo(String to){
this.to = to;
return this;
}

public String getFrom() {
return from;
}

public String getContent() {
return content;
}

public String getTo() {
return to;
}

public String getType(){
return type;
}
}

中介者抽象类

1
2
3
4
5
6
7
abstract class Mediator {
public abstract void processMessage(Message message);

public abstract void addUser(User user);

public abstract void addFileServer(FileServer fileServer);
}

用户类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class User {

/**
* 客户名称
*/
private String name;

/**
* 消息列表
*/
private List<String> messageList = new ArrayList<>();

/**
* 中介者类
*/
private Mediator mediator;

public User(Mediator mediator) {
this.mediator = mediator;
this.mediator.addUser(this);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

/**
* 发送消息
*
* @param content 消息
* @param to 消息接收方
*/
public void pushMessage(String content, String to) {
content = content + "\t\t" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss"));
this.messageList.add("me :" + content);
Message message = new Message("msg").setContent(content).setFrom(getName()).setTo(to);
this.mediator.processMessage(message);
}

/**
* 上传文件
* @param content 文件名称
*/
public void uploadFile(String content){
Message message = new Message("file").setFrom(getName()).setContent("U"+content);
this.mediator.processMessage(message);
}

/**
* 下载文件
* @param content 文件名称
*/
public void downloadFile(String content){
this.messageList.add("申请下载《"+content+"》,请求文件服务器中...");
Message message = new Message("file").setFrom(getName()).setContent("D"+content);
this.mediator.processMessage(message);
}

/**
* 接收消息
* @param content 消息
*/
protected void putMessage(String content) {
this.messageList.add(content);
}

/**
* 展示消息列表
*/
public void showMessages() {
System.out.println("\n======== " + getName() + "'s messageBox begin ========");
messageList.forEach(System.out::println);
System.out.println("======== " + getName() + "'s messageBox end ========\n");
}
}

文件服务器类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class FileServer {
List<String> fileList = new ArrayList<>();

private Mediator mediator;

public FileServer(Mediator mediator) {
this.mediator = mediator;
this.mediator.addFileServer(this);
}

private boolean isFound;

public void upload(String fileName, String userName) {
String returnContent;
if (!fileList.contains(fileName)) {
fileList.add(fileName);
returnContent = "文件上传成功";
} else {
returnContent = "文件已经存在";
}
Message message = new Message("msg").setFrom("FileServer").setContent(returnContent).setTo(userName);
mediator.processMessage(message);
}


public void download(String fileName, String userName) {
String returnContent;
isFound = false;
fileList.forEach((String file) -> {
if (file.equalsIgnoreCase(fileName)) {
isFound = true;
}
});
if (isFound) {
returnContent = "[" + fileName + "]传输中...";
} else {
returnContent = "下载失败,[" + fileName + "]不存在";
}
Message message = new Message("msg").setFrom("FileServer").setContent(returnContent).setTo(userName);
mediator.processMessage(message);
}

public void showFileList() {
fileList.forEach(System.out::println);
}
}

中介者实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class ConcreteMediator extends Mediator {

List<User> userList = new ArrayList<>();
List<FileServer> fileServerList = new ArrayList<>();

public ConcreteMediator() {
}


@Override
public void processMessage(Message message) {
String messageType = message.getType();
String content = message.getContent();
String from = message.getFrom();
String to = message.getTo();
switch (messageType) {
case "file":
fileServerList.forEach((FileServer server) -> {
if (content.startsWith("U")) {
String fileName = content.substring(1);
server.upload(fileName, from);
} else if (content.startsWith("D")) {
String fileName = content.substring(1);
server.download(fileName, from);
} else {
this.processMessage(new Message("msg").setContent("请求内容错误").setTo(from).setFrom("系统"));
}
});
break;
case "msg":
if (to == null || to.equals("")) {
userList.forEach((User u) -> {
if(!u.getName().equals(from)) {
String msg = from + ": " + content;
u.putMessage(msg);
}
});
}else{
userList.forEach((User u)->{
if(u.getName().equals(to)){
u.putMessage(from + ": "+content);
}
});
}
break;
}
}

@Override
public void addUser(User user) {
this.userList.add(user);
}

@Override
public void addFileServer(FileServer fileServer) {
this.fileServerList.add(fileServer);
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class MediatorTest {

@Test
public void test() throws Exception{
Mediator mediator = new ConcreteMediator();

FileServer fileServer = new FileServer(mediator);

User u1 = new User(mediator);
User u2 = new User(mediator);
User u3 = new User(mediator);
u1.setName("leithda");
u2.setName("mellofly");
u3.setName("吃瓜群众");

u3.uploadFile("三国演义");
u2.uploadFile("设计模式");

u1.pushMessage("Hello, mellofly, Where are you?","mellofly");
u2.pushMessage("Hi,Le,我在上海,我上传了一个文件叫《设计模式》","leithda");

u1.pushMessage("大家,mellofly上传了《设计模式》,大家快去看啊","");

u1.downloadFile("设计模式");
u2.downloadFile("三国演绎");
u2.downloadFile("三国演义");
u3.downloadFile("设计模式");

u1.showMessages();
u2.showMessages();
u3.showMessages();

fileServer.showFileList();
}
}
  • 为了好玩把例子写的太麻烦了 |T. T|

Example

Refence