-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathmain.py
41 lines (28 loc) · 906 Bytes
/
main.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
from abc import ABCMeta, abstractmethod
from datetime import datetime
class ChatRoomMediator(metaclass=ABCMeta):
@abstractmethod
def show_message(self, user, message):
pass
class ChatRoom(ChatRoomMediator):
"""Mediator"""
def show_message(self, user, message):
time = datetime.now()
sender = user.name
print(f'{time} [{sender}]: {message}')
class User:
def __init__(self, name, chat_mediator):
self.name = name
self.chat_mediator = chat_mediator
def send(self, message):
self.chat_mediator.show_message(self, message)
if __name__ == '__main__':
mediator = ChatRoom()
john = User('John', mediator)
jane = User('Jane', mediator)
josh = User('Josh', mediator)
john.send('Hi there!')
jane.send('Hi!')
john.send('How are you?')
jane.send("I'm great, thanks!")
josh.send('Hi guys!')