-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediator_pattern.py
More file actions
31 lines (24 loc) · 926 Bytes
/
mediator_pattern.py
File metadata and controls
31 lines (24 loc) · 926 Bytes
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
from abc import *
class ChatRoom():
def show_message(self, origin: str, message: str) -> None:
print(f"{origin}, {message}")
class User():
def __init__(self, name: str, chatroom: ChatRoom) -> None:
self.name = name
self.chatroom = chatroom
def send(self, message: str) -> None:
self.chatroom.show_message(origin=self.name, message=message)
class System():
def __init__(self, name: str, chatroom: ChatRoom) -> None:
self.name = name
self.chatroom = chatroom
def send(self, message: str) -> None:
self.chatroom.show_message(origin=self.__class__.__name__, message=message)
if __name__ == '__main__':
chatroom = ChatRoom()
sainz = User("Sainz", chatroom)
lewis = User("Lewis", chatroom)
system = System("System", chatroom)
sainz.send("Hello!")
lewis.send("Hey!")
system.send("Welcome!")