Mediator pattern
medium 45 minThe 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
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
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
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
criticalOne shortcut, and the web is back beside the star.
Wrongclass Username(TextInput): def typed(self): self.login_button.enable()Rightclass Username(TextInput): def typed(self): self.mediator.changed(self, "typed")The god object
criticalThe mediator does the work of its colleagues.
Wrongdef changed(self, w, event): if not re.match(r"\w+", w.text): w.border = "red"Rightdef changed(self, w, event): self.login.set_enabled(w.valid())Echo without end
criticalThe update is reported as a change, which causes an update.
Wrongdef changed(self, field): self.other(field).set(field.value)Rightdef changed(self, field): if self.updating: return self.updating = True try: self.other(field).set(field.value) finally: self.updating = FalseDelivering while holding a lock
criticalA slow or calling-back user stops the whole room.
Wrongwith self.lock: for user in self.users: user.receive(text)Rightwith self.lock: users = list(self.users)for user in users: user.receive(text)Forgetting to leave
majorThe room holds everyone who ever joined, and they are never freed.
Wrongroom.add_user(user)# the window closesRightroom.add_user(user)...room.remove_user(user)Strings for everything
majorA typing error in an event name is silently ignored.
Wrongmediator.notify(self, "chnaged")Rightmediator.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.