Mediator
Mediator centralizes collaboration rules among peers so those peers do not need to reference every other participant. It coordinates behavior, not necessarily every piece of state.
What you’ll understand
- Mediator centralizes collaboration rules among peers so those peers do not need to reference every other participant. It coordinates behavior, not necessarily every piece of state.
- A form mediator recomputes the shared enablement rule when either participant changes.
- Evaluate the tradeoff: The mediator can become too large if it absorbs domain logic unrelated to collaboration. Keep field validation and persistence in their own components.
Useful first: Classes and Objects
The idea and when to recognize it
Mediator centralizes collaboration rules among peers so those peers do not need to reference every other participant. It coordinates behavior, not necessarily every piece of state.
- Requirement: A form’s submit action depends on consent and on whether the email field contains a usable value.
- Change to anticipate: Keep submit enabled only when both independent field conditions are satisfied.
- Design goal: A form mediator recomputes the shared enablement rule when either participant changes.
Start with the concrete problem
A form’s submit action depends on consent and on whether the email field contains a usable value.
Keep submit enabled only when both independent field conditions are satisfied.
| Direct implementation | Revised design |
|---|---|
| Letting the most recently edited field enable submit alone ignores the other field’s state. | A form mediator recomputes the shared enablement rule when either participant changes. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | The mediator can become too large if it absorbs domain logic unrelated to collaboration. Keep field validation and persistence in their own components. |
Responsibilities and contracts
A form mediator recomputes the shared enablement rule when either participant changes.
- FormMediator: Coordinates field conditions. Public operations: canSubmit()
- EmailField: Owns email text. Public operations: value
- ConsentField: Owns consent. Public operations: checked
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A dialog mediator can coordinate a search box, result list, and preview without direct peer references.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
The mediator can become too large if it absorbs domain logic unrelated to collaboration. Keep field validation and persistence in their own components.
Common mistake: Letting the most recently edited field enable submit alone ignores the other field’s state.
- Explain the choice: A form mediator recomputes the shared enablement rule when either participant changes.
- Challenge your choice: The mediator can become too large if it absorbs domain logic unrelated to collaboration. Keep field validation and persistence in their own components.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…