-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4c7d46
commit 67863c9
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...patterns/src/main/java/io/walkers/planes/pandora/design/patterns/mediator/ChatServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.walkers.planes.pandora.design.patterns.mediator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 中介者角色:聊天服务器 | ||
* | ||
* @author planeswalker23 | ||
* @date 2022/2/22 | ||
*/ | ||
public class ChatServer { | ||
|
||
private List<User> users = new ArrayList<>(); | ||
|
||
public void register(User user) { | ||
users.add(user); | ||
user.setChatServer(this); | ||
System.out.println("用户" + user.getUsername() + "加入聊天室"); | ||
} | ||
|
||
public void reply(Message message) { | ||
for (User user1:users) { | ||
if (message.getTargetGroup().equals(user1.getGroup())) { | ||
user1.receive(message); | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...gn-patterns/src/main/java/io/walkers/planes/pandora/design/patterns/mediator/Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.walkers.planes.pandora.design.patterns.mediator; | ||
|
||
/** | ||
* @author planeswalker23 | ||
* @date 2022/2/22 | ||
*/ | ||
public class Message { | ||
// 发送内容 | ||
private String content; | ||
// 发送组 | ||
private String targetGroup; | ||
// 发送者 | ||
private User sender; | ||
|
||
public Message(String content, String targetGroup, User sender) { | ||
this.content = content; | ||
this.targetGroup = targetGroup; | ||
this.sender = sender; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
|
||
public String getTargetGroup() { | ||
return targetGroup; | ||
} | ||
|
||
public void setTargetGroup(String targetGroup) { | ||
this.targetGroup = targetGroup; | ||
} | ||
|
||
public User getSender() { | ||
return sender; | ||
} | ||
|
||
public void setSender(User sender) { | ||
this.sender = sender; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
design-patterns/src/main/java/io/walkers/planes/pandora/design/patterns/mediator/Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.walkers.planes.pandora.design.patterns.mediator; | ||
|
||
/** | ||
* @author planeswalker23 | ||
* @date 2022/2/22 | ||
*/ | ||
public class Test { | ||
public static void main(String[] args) { | ||
ChatServer chatServer = new ChatServer(); | ||
User user1 = new User("user1", "A"); | ||
User user2 = new User("user2", "A"); | ||
User user3 = new User("user3", "B"); | ||
chatServer.register(user1); | ||
chatServer.register(user2); | ||
chatServer.register(user3); | ||
|
||
Message message = new Message("你好", "A", user3); | ||
user3.send(message); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
design-patterns/src/main/java/io/walkers/planes/pandora/design/patterns/mediator/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.walkers.planes.pandora.design.patterns.mediator; | ||
|
||
/** | ||
* 同事角色:用户 | ||
* | ||
* @author planeswalker23 | ||
* @date 2022/2/22 | ||
*/ | ||
public class User { | ||
// 用户昵称 | ||
private String username; | ||
// 用户群组 | ||
private String group; | ||
// 用户所属聊天服务器 | ||
private ChatServer chatServer; | ||
|
||
public User(String username, String group) { | ||
this.username = username; | ||
this.group = group; | ||
} | ||
|
||
// 接收消息 | ||
public void receive(Message message) { | ||
System.out.println("用户" + this.username + "收到用户" + message.getSender().getUsername() + "发来的消息,内容为:" + message.getContent()); | ||
} | ||
|
||
// 发送消息 | ||
public void send(Message message) { | ||
System.out.println("用户" + this.username + "向群组" + this.group + "的用户发出消息,内容为:" + message.getContent()); | ||
chatServer.reply(message); | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getGroup() { | ||
return group; | ||
} | ||
|
||
public void setGroup(String group) { | ||
this.group = group; | ||
} | ||
|
||
public ChatServer getChatServer() { | ||
return chatServer; | ||
} | ||
|
||
public void setChatServer(ChatServer chatServer) { | ||
this.chatServer = chatServer; | ||
} | ||
} |