Mediator pattern 00:00:00 Make a prediction You will move coordination rules out of peer controls without turning the coordinator into the owner of unrelated business logic. Changing a country updates both the shipping choices and the postcode format. Should the country selector hold references to every field it might eventually affect? It can, but each new dependent adds another direct coupling. Report the selection to a mediator that owns the form's coordination rule and updates the relevant controls. 00:00:32 Asha and the dancing tugboats Several tugboats cannot each pull wherever they think best. A harbour guide watches their positions and signals who should move next. The guide coordinates their moves without becoming each boat's engine. The mediator owns relationships between participants. The participants keep local behaviour; routing every domain computation through the mediator would make it unwieldy. The Mediator pattern is a behavioral design pattern. A mediator contains the interaction of a set of colleagues. Colleagues hold a reference to the mediator and to no other colleague. Without it, the cooperation is spread over all parts. Nobody can read it, each part drags its neighbours along, and a new part changes the old ones. 00:01:18 Coordinate a checkout form Changing country changes postcode validation and available shipping choices. If every control holds references to every other control, adding one field spreads dependencies. Let the selector report a country change to the form mediator. The mediator applies the coordination rule and updates the postal field and shipping choices. Exercise a country switch and observe the two dependent controls. Keep tax computation outside the mediator so it does not become the whole application. Select a new country while the old postal code is still present. The mediator revalidates against the new format and updates submit availability. If each widget tells every other widget directly, one change can trigger a circular update. Trace the resulting UI until it settles. 00:02:07 Coordinate chat-room participants Each participant sends a message to a room. The room selects recipients and delivers it, so a participant can leave without everyone updating peer references. A single logical mediator simplifies communication; it does not imply the entire deployment must use one process. Let one member send a message while a second leaves. The chat mediator decides which membership snapshot determines recipients. If each participant maintains a private peer list, two different people may believe the departed user is still present. Centralizing routing makes that choice testable. If the mediator also calculates tax, charges the card and stores every user record, it becomes a god object. Its useful boundary is coordination among these participants. One simple peer callback is fine when there is no many-to-many dependency to tame. Swing ButtonGroup. The documentation calls it a multiple-exclusion scope: turning on one button turns off all others in the group. The buttons do not know each other. HTML radio groups. Radio inputs with the same name form a group in which only one can be selected. The browser is the middle. 00:03:23 Try an interview follow-up Two users send at the same time. What do you guarantee? I would give all room members the same event order. A single ordering stage assigns a sequence number and places the event into each eligible member's queue. Delivery can then take place outside the shared room lock. Sending under that lock lets one slow connection stop the entire room. Changing a country field changes the valid postal-code format and may disable a submit button. Put that interaction rule in a dialog mediator. Trace a field change through validation to the button. Check that updating the button cannot trigger the same field-change event again. 00:04:07 What to take with you Colleagues report facts, the mediator decides, colleagues do the work. The missing arrows between colleagues are the pattern. Count honestly: links against links, a draw at three. The real gain is that rules between objects have one home that can be tested.