CSC/ECE 517 Fall 2012/ch2b 2w43 iv: Difference between revisions
Line 19: | Line 19: | ||
====Case1: Without the use of Mediator Pattern==== | ====Case1: Without the use of Mediator Pattern==== | ||
[[File:Image1_womediator1.png|650 px|thumb|right|Example: Without using Mediator Pattern]] | [[File:Image1_womediator1.png|650 px|thumb|right|Example: Without using Mediator Pattern]] | ||
In this example, there are four departments of a hospital, namely :Pathology, Radiology, Surgery and Physiology. When one department needs some data from another department, they need to explicitly contact the other department for that data. Since these departments directly interact with each other, the communication between these parties is very complex and results in high coupling. | In this example, there are four departments of a hospital, namely :Pathology, Radiology, Surgery and Physiology. When one department needs some data from another department, they need to explicitly contact the other department for that data. Since these departments directly interact with each other, the communication between these parties is very complex and results in high coupling. | ||
====Case2: With the use of Mediator Pattern==== | |||
[[File:Image2_withmediator.png|650 px|thumb|right|Example: Using Mediator Pattern]] | |||
To reduce coupling in the above example we add a |
Revision as of 01:20, 17 November 2012
Mediator Pattern
Introduction
Mediator pattern comes under the category of behavioral pattern. It specifies an object that includes the encapsulation about the interaction among various objects.
It prevents objects to interact with each other explicitly and thus it facilitates loose coupling. In other words it handles complex communication among related objects. All the objects communicate with the mediator when they want to interact with each other. Thus it improves maintainability.
All the classes which interact with the mediator are called colleagues. These colleagues know who their mediator is and the mediator also knows which colleagues can interact with it. The colleagues send messages to the mediator object. The mediator sends message to other classes. Thus the object which initiated this communication does not need to have knowledge about other objects. All they need to know is about their mediator who is responsible to carry out this communication.
Following classes/objects take part in this process:
Mediator: Provides interface for communication among colleagues.
Concrete Mediator: It coordinates the colleagues and implements cooperative behavior. It also holds information about the colleagues.
Colleague Classes: Colleague classes know about their mediator.
A real life example of mediator pattern can be of an airport control tower. It is the job of this tower to control all the planes. It decides which plane will take-off and land at what time rather than plane to plane communication.
Case1: Without the use of Mediator Pattern
In this example, there are four departments of a hospital, namely :Pathology, Radiology, Surgery and Physiology. When one department needs some data from another department, they need to explicitly contact the other department for that data. Since these departments directly interact with each other, the communication between these parties is very complex and results in high coupling.
Case2: With the use of Mediator Pattern
To reduce coupling in the above example we add a