Python: Mediator Pattern

DuMediator.py

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
# 中介者模式  Mediator Pattern
 
import sys
 
# ParticipantReference:
class User(object):
    def __init__(self, med, name):
        self.mediator = med
        self.name = name
 
    def send(self, msg):
        pass
 
    def receive(self, msg):
        pass
 
 
# The Mediator:
class ChatMediator(object):
    def __init__(self):
        self.users = []
 
    def add_user(self, user):
        self.users.append(user)
 
    def send_message(self, msg, user):
        for u in self.users:
            if u != user:
                u.receive(msg)
 
# Participant
class ConcreteUser(User):
 
    def send(self, msg):
        print(self.name + ": 发送消息: " + msg)
        self.mediator.send_message(msg, self)
 
    def receive(self, msg):
        print(self.name + ": 收到的消息: " + msg)
 
 
# geovindu: two
class ChatRoom(object):
    '''Mediator class.'''
 
    def __init__(self):
        self.users = []
 
    def add_user(self, user):
        self.users.append(user)
 
    def displayMessage(self, user, message):
        print("[{} 说]: {}".format(user, message))
 
 
class User(object):
    '''A class whose instances want to interact with each other.'''
 
    def __init__(self, name):
        self.name = name
        self.chatRoom = ChatRoom()
 
    def sendMessage(self, message):
        self.chatRoom.displayMessage(self, message)
 
    def __str__(self):
        return self.name

 

 

main.py 调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 中介者模式  Mediator Pattern
mediator = DuMediator.ChatMediator()
userDu = DuMediator.ConcreteUser(mediator,"GeovinDu")
userDu2 = DuMediator.ConcreteUser(mediator,"geovindu")
userDu3 = DuMediator.ConcreteUser(mediator,"涂聚文")
userDu4 = DuMediator.ConcreteUser(mediator,"geovin")
 
mediator.add_user(userDu)
mediator.add_user(userDu2)
mediator.add_user(userDu3)
mediator.add_user(userDu4)
 
userDu.send("Hello 2024 程序员节节日快乐.")
print("\n")
GeovinDu = DuMediator.User("GeovinDu")
GeovinDu2 = DuMediator.User("geovindu")
GeovinDu3 = DuMediator.User("涂聚文")
GeovinDu4 = DuMediator.User("geovin")
GeovinDu.sendMessage("你好,见到你真高兴.程序员节节日快乐")
GeovinDu2.sendMessage("你好,见到你真高兴.程序员节节日快乐")
GeovinDu3.sendMessage("你好,见到你真高兴.程序员节节日快乐")
GeovinDu4.sendMessage("你好,见到你真高兴.程序员节节日快乐")

 

 

 

输出:

1
2
3
4
5
6
7
8
9
10
GeovinDu: 发送消息: Hello 2024 程序员节节日快乐.
geovindu: 收到的消息: Hello 2024 程序员节节日快乐.
涂聚文: 收到的消息: Hello 2024 程序员节节日快乐.
geovin: 收到的消息: Hello 2024 程序员节节日快乐.
 
 
[GeovinDu 说]: 你好,见到你真高兴.程序员节节日快乐
[geovindu 说]: 你好,见到你真高兴.程序员节节日快乐
[涂聚文 说]: 你好,见到你真高兴.程序员节节日快乐
[geovin 说]: 你好,见到你真高兴.程序员节节日快乐

 

 

posted @   ®Geovin Du Dream Park™  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2011-10-24 Csharp windowform bindingNavigator,bindingSource,DataGridView簡單分頁:首頁,上一頁,下一頁,末頁
2009-10-24 World Currency Symbols世界货币符号
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示