Download presentation
Presentation is loading. Please wait.
Published byMelvyn Small Modified over 8 years ago
2
Behavioral Pattern: Mediator C h a p t e r 5 – P a g e 169 When a program is made up of several classes, the logic and computation is divided among these classes. However, as more classes are developed, the problem of communication between them becomes more complex. This makes the program harder to read and to maintain. Further, it can become difficult to change the program, since any change may affect code in several other classes. The Mediator Pattern addresses this problem by promoting looser coupling between the classes. The Mediator is the only class that has detailed knowledge of the methods of other classes. Classes inform the Mediator when changes occur and the Mediator passes them on to any other classes that need to be informed.
3
The Mediator Pattern C h a p t e r 5 – P a g e 170 The Mediator provides the interface for communicating with Colleague objects. The ConcreteMediator knows and maintains its Colleagues, and implements cooperative behavior by coordinating the Colleague objects. Each Colleague class knows its Mediator object, communicating with it whenever it would have otherwise communicated with another Colleague.
4
C h a p t e r 5 – P a g e 171 Non-Software Example: ATC The pilots of the planes (the Colleagues) approaching or departing the terminal area communicate with the tower (the Mediator) rather than explicitly communicating with one another. The constraints on who can take off or land are enforced by the tower. Notice that the tower does not control the whole flight. It exists only to enforce constraints in the terminal area.
5
C h a p t e r 5 – P a g e 172 Software Example: Label Switched Paths Consider a complex managed network, offering a variety of services, including Internet telephony and data applications (email, server access, application access, etc.). A label switched path (LSP) is a forwarding path for traffic. The advantage of using LSPs rather than merely relying on IP forwarding is that LSPs provide more detailed control of the network. Path and Quality-of-Service (QoS) configuration data (the Colleagues) is dynamically maintained by a connection director (the Mediator) who continually adjusts and reconfigures the LSP.
6
C h a p t e r 5 – P a g e 173 LSP Mediator Code in C++ #include using namespace std; class Widget; class ConnectionDirector { public: explicit ConnectionDirector() {} virtual ~ConnectionDirector() {} virtual void WidgetChanged(Widget*) = 0; protected: ConnectionDirector(int); virtual void CreateWidgets() = 0; }; class Widget { public: explicit Widget(ConnectionDirector* aConnDirector) { director = aConnDirector; } virtual void Changed() { director->WidgetChanged(this); } protected: ConnectionDirector* director; };
7
C h a p t e r 5 – P a g e 174 class PathWidget : public Widget { public: explicit PathWidget(ConnectionDirector* theDirector) : Widget(theDirector) {} virtual ~PathWidget() {} void CreateInitialPath(char* newPath) { pathDetails = newPath; cout << "Initial path setting: " << pathDetails << endl; } void getNewPath() { cout << "The PathWidget path is: " << pathDetails << endl; } void ChangePath(char* newPath) { pathDetails = newPath; cout << "The updated PathWidget path is: " << pathDetails << endl; Changed(); } private: char* pathDetails; };
8
C h a p t e r 5 – P a g e 175 class QosWidget : public Widget { public: explicit QosWidget(ConnectionDirector* theDirector) : Widget(theDirector) {} virtual ~QosWidget() {} void CreateInitialQoS(char* initialQoSDetails) { QoSDetails = initialQoSDetails; cout << "Initial QoS setting: " << QoSDetails << endl; } void getNewQoS() { cout << "The QosWidget details are: " << QoSDetails << endl; } void ChangeQoSDetails(char* newQoSDetails) { QoSDetails = newQoSDetails; cout << "The updated QosWidget QoS details are " << QoSDetails << endl; Changed(); } private: char* QoSDetails; };
9
C h a p t e r 5 – P a g e 176 class LspDirector : public ConnectionDirector { public: explicit LspDirector(int anLspId) {lspId = anLspId; } virtual ~LspDirector() {} virtual void WidgetChanged(Widget* imAChangedWidget) { if (imAChangedWidget == lspPath) // Change the path used by the LSP cout << "Director informed of change to LSP Path" << endl; else if (imAChangedWidget == qosDescriptor) // Change the Qos resources used by the LSP cout << "Director informed of change to QoS Resources" << endl; } virtual void CreateWidgets() { lspPath = new PathWidget(this); lspPath->CreateInitialPath("R2R3R4R5"); qosDescriptor = new QosWidget(this); qosDescriptor->CreateInitialQoS("Assured Forwarding"); } void ChangePath(char* newPath) { lspPath->ChangePath(newPath); } void ChangeQoSDetails(char* newQoS) { qosDescriptor->ChangeQoSDetails(newQoS); } private: int lspId; PathWidget* lspPath; QosWidget* qosDescriptor; };
10
C h a p t e r 5 – P a g e 177 void main() { LspDirector* anExampleMediator = new LspDirector(555); // Create several widgets for the LSP anExampleMediator->CreateWidgets(); anExampleMediator->ChangePath("R2R6R5"); anExampleMediator->ChangeQoSDetails("Expedited Forwarding"); delete anExampleMediator; return; }
11
Mediator Pattern Advantages C h a p t e r 5 – P a g e 178 The Mediator makes loose coupling possible between objects in a program. It also localizes the behavior that otherwise would be distributed among several objects. The Mediator approach makes it possible to add new Colleagues to a system without having to change any other part of the program. In addition, the behavior of the program can be changed by simply changing or subclassing the Mediator. The Mediator can become monolithic in complexity, making it hard to change and maintain. This situation can sometimes be improved by revising the responsibilities given to the Mediator. Each object should carry out its own tasks and the Mediator should only manage the interaction between objects.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.