Mediator Design Pattern


GoF Statement: Defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and lets you vary their interaction independently.
Category: Behavioral
UML Diagram:  

Description of the Demonstration:

Many of the appliances in this "house" must interact with one another. Rather than having to create multiple interconnections any of which may be affected by a change in one, all of the appliances communicate via the HomeMediator. For example, when the Sprinkler needs to know the time and date it asks the HomeMediator to get the information for it. The HomeMediator then gets the time from the AlarmClock and the date from the Calendar, neither of which need be concerned with what appliance is needing that information. Sprinkler also doesn't need to know where the information it got back came from, thus loose coupling is promoted among all the appliances.


Code:

Click here to download a zip file containing all the source files and an executable of the Demonstration. See the _ReadMe.txt file in the zip.