|Lesson 12||Mediator: consequences |
|Objective||Describe the Benefits and Pitfalls of the Mediator Pattern.|
Describe Benefits and Pitfalls of the Mediator Pattern
In order to have good object oriented design we have to create classes interacting one with each other.
If certain principles are not applied the final framework will end in chaos where each object relies on many other objects in order to run. In order to avoid tightly coupled frameworks, we need a mechanism to facilitate the interaction between objects in such a way that objects are not aware of the existence of other objects. Let's take the example of a screen. When we create the screen we add controls to the screen. This control needs to interact with all the other control. For example when a button is pressed, it must know if the data is valid in other controls. As you have seen if you created different applications using forms you don't have to modify each control class each time you add a new control to the form.
All the operations between controls are managed by the form class itself. This class is called mediator.
Advantages of the Mediator Pattern
- Orchestrated Harmony: Just as a conductor harmonizes the various instruments in an orchestra, the Mediator Pattern brings order and coordination among disparate objects in a system. It centralizes external communications, which allows objects to remain ignorant of the complex relationships they are a part of, ensuring that each object plays its part seamlessly.
- Reduced Object Dependencies: Imagine a social gathering where everyone converses only through a single host. The Mediator acts as that host, reducing the need for every object to directly interact with every other object. This isolation allows one to be altered, added, or removed without necessitating changes in others, making the ensemble highly modular and easier to manage.
- Enhanced Object Reusability: Objects in a Mediator Pattern become akin to solo performers able to play in multiple orchestras. Because they are decoupled from the system they serve, they can be reused in different contexts with little to no modification, much like a versatile actor taking on various roles yet keeping the essence of their craft intact.
- Simplified Maintenance and Lowered Complexity: The Mediator acts as a gatekeeper or a hub, making it easier to understand and manage the system's overall behavior. Like a well-designed traffic intersection reduces road accidents, the Mediator simplifies the path of communication, making it easier to navigate the complex web of object interactions during maintenance.
Disadvantages of the Mediator Pattern
- Centralization of Control:
The Mediator becomes the omnipotent director of the show, and if it fails, the entire system grinds to a halt. This single point of control can be both a bottleneck for performance and a single point of failure, much like how a conductor’s misstep could throw the entire orchestra out of sync.
- Complexity of the Mediator:
The Mediator, in its quest to simplify relationships among objects, can become an epitome of complexity itself. Imagine a diplomat entangled in the political intricacies of multiple nations; the Mediator similarly risks becoming overly complex as it takes on more responsibilities, making it harder to maintain.
- Potential for Feature Envy:
In some cases, the Mediator might need to access too much information about the objects it's coordinating, resembling a gossip monger who knows too much about everyone. This can lead to a situation called 'Feature Envy,' where the Mediator is too involved in the affairs of other classes, leading to potential issues in encapsulation.
- Risk of Monolithism: While the Mediator aims to bring harmony, it can unintentionally turn into a monolithic entity that is resistant to change. Much like a rigid bureaucracy that becomes an obstacle rather than a facilitator, a poorly designed Mediator can stifle the very flexibility and modularity it aims to bring.
In summary, the Mediator Pattern, like a skillful maestro, can bring out the best in a system by enhancing modularity and simplifying interactions. Yet, it carries the risk of becoming a complicated or overbearing figure that may create more problems than it solves. Therefore, it requires careful orchestration to yield its benefits while minimizing its drawbacks.
Coupling describes the degree of dependency between one entity to another entity. Often classes or objects.