Answer the question
In order to leave comments, you need to log in
Is this a good example of the mediator pattern?
Here is an example using the mediator pattern:
class ChatRoom(object):
"""Mediator class"""
def display_message(self, user, message):
print("[{} says]: {}".format(user, message))
class User(object):
"""A class whose instances want to interact with each other"""
def __init__(self, name):
self.name = name
self.chat_room = ChatRoom()
def say(self, message):
self.chat_room.display_message(self, message)
def __str__(self):
return self.name
def main():
molly = User('Molly')
mark = User('Mark')
ethan = User('Ethan')
molly.say("Hi Team! Meeting at 3 PM today.")
mark.say("Roger that!")
ethan.say("Alright.")
if __name__ == '__main__':
main()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question