Design Patterns

Mediator



Mediator

Type: Behavioral
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 lets you vary their interaction independently.
UML Diagram:


Design Pattern Type: Behavioral

GoF Statement of 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 lets you vary their interaction independently.
Brief Overview:



Provides a unified interface to a set of interfaces in a subsystem. So it is similar to the façade. This is a behavioral pattern due to the way it can alter the program's running behavior. Usually a program is made up of a large number of classes which handle different logic and computational tasks. As more classes are added during maintenance and/or refactoring the problem of communication between these classes becomes more complex and difficult to change. The mediator encapsulates communication between objects so that objects no longer communicate directly with one another but via the mediator. This reduces dependencies between objects and improves decoupling.
UML Diagram:

Discussion and In-class Example:

In this example Bob, the Computer Geek, has a home control system. When he stops hitting the snooze button on his alarm clock it tells the coffee maker to start brewing. But, Bob keeps asking for more features, like "No coffee on weekends", "Turn off sprinkler 15 minutes before a shower is due", "Don’t water on scheduled days if it is raining", and so on.

This really complicates the organization of the code for his home control system. So the designers change the layout and organization of the software by adding a Mediator through which all the systems communicate.

With this new design: