Reading Materials
- https://en.wikipedia.org/wiki/Mediator_pattern (15 minutes reading)
Practice Materials
- Extend the Command Pattern exercise by applying the Mediator Pattern to a new
SignAllCommand
class, which delegate the request to a mediator that does the actual business logic.
If you haven't done the Command Pattern Exercise but you are already familiar with the Command Pattern, you can go straight to this exercise.
Components:
- Command (The common interface that has the
execute
andundo
methods) or Colleague -Command
- ConcreteCommand (The actual command that implements the
Command
interface, and takes a receiver and calls its member methods.) -SignAllCommand
andWriteCommand
- Client (The entry class that creates the command and its receiver) -
App
- Receiver (The class that will be used in a command) -
Document
- Invoker (The class that invokes a command) -
DocumentRunner
- Stack (The object that keeps track of most recent executed commands. To undo, the stack just pops the last commands and executes its
undo
method.) -FixedStack
- Mediator (The component where all cross-cutting logic happens) -
Mediator
Tip
- Without using a mediator in this exercise, the SignAllCommand will have to hold the state of all the documents passed in. As the DocumentWriter application grows, more commands are introduced to the application, among which there may be similar group commands similar to the SignAllCommand that will have to hold other references of all the documents, which add unnecessary duplicated references across all group commands. A mediator could be a hub that holds the references to all documents and related operations. There could be more discussions on how to submodularize the mediator so that it doesn't grow into a super long class with all different kind of tasks.
Solution
Extended Operation Manager Example -- A Document Writer
Questions to discuss
- What are the common use cases to apply a Mediator Pattern?
- How do we submodularize a mediator so that it doesn't grow into a class has too many responsibilities?