This is the 15th day of my participation in the August Text Challenge.More challenges in August
broker
The mediator pattern: Use the mediator to centralize the complex communication and control patterns between related objects.
Bob has an automatic house. When his alarm clock goes off, it tells the coffee maker to start making coffee. Later, customers constantly put forward many new requirements: do not provide coffee on weekends, need to sweep the floor robot after work, etc. At this time, the coffee maker needs to accept the alarm clock and check the date, while the calendar needs to accept the alarm clock and tell the alarm clock whether it is weekend, and the sweeping robot needs to accept the alarm clock and check whether it is off time and a series of operations.
Trying to keep track of every rule for every object, and the intricate relationships between many objects, does not satisfy a single responsibility for design, and can complicate the system by referring to each other. At this point, you can use the mediator pattern to introduce a mediator into the system.
- Each object tells the mediator when its state changes.
- Each object responds to a request made by the mediator.
Design Class diagram:
1) Mediator abstract is used to define a unified interface for interaction between equipment objects. 2) Concrete Mediator holds the reference of each equipment object and is responsible for coordinating the behavior of each equipment object to complete cooperation. Each device knows about the intermediary object (holding interface references) and communicates with other devices through the collaboration of the intermediary. Each device implements interface for intermediary notification
Advantages:
1. By decoupling objects from each other, you can increase their reusability.
2. By centralizing the control logic, system maintenance can be simplified.
3. You can simplify and greatly reduce the number of messages passed between objects.
Usage and Disadvantages
Mediators are often used to coordinate related GUI components.
2. The downside of mediators is that the mediator object itself can become too complex if not designed properly.
The memo
Memo mode: Use memo mode whenever you need to return an object to its previous state (for example, undo)
For example, in console games we often play, when the user moves to the next level, we die in the game, usually at the beginning of the level. When we move to the next level, it’s a memo mode that lets us stay where we were at the last level.
The memo pattern has two goals:
- Stores the important status of key objects in the system.
- Maintain encapsulation of key objects.
Don’t forget to have a single responsibility, and don’t confuse staying on track with your key objectives. This state-specific object is called a memo.
Design Class diagram:
Advantages:
Keeping stored state out of the way and not mixing it with key objects helps maintain cohesion.
2. Maintain data encapsulation of key objects.
3. Provide resilience that can be easily implemented.
Usage and disadvantages:
1. Memos are used to store state.
2. Disadvantages of using memos: The process of storing and restoring state can be time-consuming.
3. Serialization mechanism can be used to store system objects during system design.