Mediator
Mediator Intent Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. (Behavioral) 2
When objects in a subsystem interact, they often become tightly coupled. When something happens here… When something happens here… 3
Mediator lets you separate out the coordination behavior to one location. OK. I will coordinate the subsystem changes. Mediator I had a state change. I had a state change. 4
The Mediator pattern has a very simple structure. mediator Mediator Colleague 1 * Coordination behavior is all in here. ConcreteColleague1 ConcreteMediator An object ConcreteColleague2 Set of objects 5
The coordination activity all resides in the mediator relieving subsystem objects of this duty. mediator Mediator Colleague 1 * ConcreteColleague1 ConcreteMediator State change ConcreteColleague2 6
Controlling a bank of elevators is an application that could benefit from use of the Mediator pattern. Set of objects • Elevators, floor buttons • Floor lobby call signals Coordinating behaviors • Morning rush, lunch, evening • Weekday • Weekend • Emergency 7
You can mediate using the stock pattern with a little help from an observer. Observer Subject mediator ElevatorSequencing ElevatorComponent 1 * Elevator MorningRush Elevator LunchRush Elevator EveningRush FloorLobby Weekday FloorLobby Weekend FloorLobby Emergency 8
And maybe if you go up one layer, this is part of something too. Context ElevatorBank carSequencer mediator ElevatorSequencing ElevatorComponent 1 * Strategy Elevator MorningRush Elevator LunchRush Elevator EveningRush FloorLobby Weekday FloorLobby Weekend FloorLobby Emergency 9
Recommend
More recommend