Wednesday, December 27, 2006
About Mediator Pattern
Why Mediator Pattern is necessary ? (When to use Mediator Pattern ?)
-> In many cases, the functionality of a system is implemented by collaboration of several objects. For example, A dialogue box of a GUI system usually contains "buttons", "text areas", "radio buttons" and so on. In the dialogue box, the components are interrelated. Pressing a button might trigger change of the content of a text area object. To implement this relationship, the objects composing the dialogue box should know each other. That is, button object should know the interface of text area object, text area object to know radio button interface and so on. It results in a monolithic line of code where many objects are interrelated. It prohibits reusability and extendibility.
How Mediator Pattern solves the problem ?
-> The Mediator Pattern rearranges communication between the objects that constitude the system. It uses "Mediator Object" to provide single communication channel between objects. The components no longer communicate each other directly. Instead, they communicate with Mediator Object. The Mediator object knows all about the subcomponents and provide proper communication methods to each object. In Dialogue Box example, the "button object" no longer communicates directly with "text area" object. When the button is pressed, it requests "change of the content of text area object" to Mediator Object.
What benefits ?
-> Since the Mediator Object deals with interaction between subcomponents, the subcomponents don't need to know each other. This simplifies implementation of subcomponents and improves reusability. It also improves "maintainability" of the software, because the programmer can concentrate on the single communication channel (the Mediator Object) when there are needs for modifying or extending the functionality of the system.
Reference.
[1] The Design Patterns, Erich Gamma et al, 1995.