Mediator: A wonderful pattern

I'm a great fan of the Strategy Pattern. Its one of those "good ideas" of OOP thats seems so basic you wonder why you never thought of it before. The problem with employing Strategy for me has always been the best way to achieve interclass communication without compromising their loose coupled architecture. My great escape has always been the MVC triad since it is a compound of the Strategy, Composite and Observer pattern (Smalltalk). Then there is the option of a Delegation Event model - nicely explained by Mr Moock in his AS2 tome.


However the the "Daddy" of all the alternatives is the Mediator. Simply put, "The mediator object: encapsulates all interconnections, acts as the hub of communication, is responsible for controlling and coordinating the interactions of its clients, and promotes loose coupling by keeping objects from referring to each other explicitly." Loose coupling between each class is achieved by having the classes communicate with the Mediator, rather than with each other.

It works at its most rudimenary in the following fashion, ClassA is composed of a couple of classes, ClassB and ClassC. The nature of the application requires that instructions need to be passed from B to C, yet they need to remain completely independent of each other. Enter the Mediator, that "mediates" between both B and C by standing as an intermediary and passing instructions from B to C and vice-versa.
The only "fly in the ointment" is the fact that Mediators do not seem to be very reusable, but then neither are Models.

0 comments:

My Instagram