AlgoViz

Mediator pattern

medium 45 min

The Mediator pattern puts the rules of how a group of objects works together into one object in the middle.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:27

See it run

Objects at run time: every call has the mediator at one end.

Operations

Chat room

Alice, Bob and Charlie talk, and nobody holds a reference to anybody.

Step 1 of 9

An empty room

roomChatRoomusers0messages0

The mediator exists first. It holds no users.

Step 1: An empty room

class ChatRoom:    def add_user(self, user):        self._users.append(user)    def send_message(self, text, sender):        for user in self._users:            if user is not sender:                user.receive(sender.name, text)class ChatUser:    def send(self, text):        self._room.send_message(text, self)    def receive(self, name, text):        self.inbox.append((name, text))
Watch
Both ends of each call
Result
Never user to user

Look for an arrow between two users. There is none in any step.

In the real world

The same mediator pattern doing real jobs. Pick one and step through it.

A chat room that takes one more rule every month, and the way out.

Step 1 of 4

Month 1

roomChatRoommethods2rules inside1

Join and send. Easy to read.

Step 1: Month 1

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A colleague that knows another

    critical

    One shortcut, and the web is back beside the star.

    Wrong
    class Username(TextInput):    def typed(self):        self.login_button.enable()
    Right
    class Username(TextInput):    def typed(self):        self.mediator.changed(self, "typed")
  • The god object

    critical

    The mediator does the work of its colleagues.

    Wrong
    def changed(self, w, event):    if not re.match(r"\w+", w.text):        w.border = "red"
    Right
    def changed(self, w, event):    self.login.set_enabled(w.valid())
  • Echo without end

    critical

    The update is reported as a change, which causes an update.

    Wrong
    def changed(self, field):    self.other(field).set(field.value)
    Right
    def changed(self, field):    if self.updating: return    self.updating = True    try: self.other(field).set(field.value)    finally: self.updating = False
  • Delivering while holding a lock

    critical

    A slow or calling-back user stops the whole room.

    Wrong
    with self.lock:    for user in self.users:        user.receive(text)
    Right
    with self.lock:    users = list(self.users)for user in users:    user.receive(text)
  • Forgetting to leave

    major

    The room holds everyone who ever joined, and they are never freed.

    Wrong
    room.add_user(user)# the window closes
    Right
    room.add_user(user)...room.remove_user(user)
  • Strings for everything

    major

    A typing error in an event name is silently ignored.

    Wrong
    mediator.notify(self, "chnaged")
    Right
    mediator.text_changed(self)

Interview tips

  • Star, not web

    • Colleagues know the mediator.
    • The mediator knows colleagues.
    • No link at the side.
  • Facts in, orders out

    • A colleague reports what happened.
    • The mediator decides.
    • The colleague does its own work.
  • Keep it thin

    • Coordination only.
    • One mediator for one topic.
    • Filters and history go out.
  • Count first

    • Two objects: call directly.
    • Four or more: think about it.
    • Rules between them: yes.
  • In production

    • One event at a time.
    • A queue for each receiver.
    • Join, leave and fail.