Z
Z
zlodiak2019-03-11 15:50:02
Python
zlodiak, 2019-03-11 15:50:02

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()

Please tell me if this is a good example to illustrate how this pattern works? Two things confuse me:
1. the mediator object does not exist in a single instance
2. objects of the User class do not interact with each other, and the mediator should help exactly in this
ps:
The fact is that I'm looking for a clear and illustrative example of this pattern, the given solution by far the most confusing in my opinion

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question