Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

C++ Mediator Pattern for Object Interaction

  • Print
  • + Share This
Monolithic software products (those with a multitude of highly coupled components) are often blamed on the use of procedural languages, but it's just as easy to produce overly interdependent classes in object-oriented languages. Stephen B. Morris shows how using the mediator design pattern can help reduce class interdependencies, aid componentization, and ultimately help make classes service-oriented.
Like this article? We recommend

Perhaps the greatest promise of object-oriented development was the move away from monolithic software (software with a multitude of highly coupled components). The problem with monolithic code is that the large number of cross connections between components makes it very hard to change or maintain. I reckon we’ve all fallen foul of such software!

Object-oriented languages and design approaches promised to change all that! The surprising thing is that it’s just as easy to create monolithic C++ or Java applications. Unfortunately, long experience teaches that it’s all too easy to produce bad software! As a great chess player once remarked: "The mistakes are all there waiting to be made." The corollary is that all the good moves are also there waiting to be made. So, with a little thought and planning, we all can produce excellent software.

Object Interactions

In software, the area in which individual objects interact seems to present much difficulty. If we get the interaction patterns wrong, the possibility of successful reuse and componentization is also reduced. The mediator design pattern provides an elegant solution to the problem of object interaction. Objects that need to communicate with each other use a mediator object for all interactions. The mediator then handles the communication with target objects – in effect the mediator becomes an intermediary. Interdependent objects know only about the mediator—they have little or no knowledge about the other objects. In short, the mediator facilitates decoupling.

A sneak preview of the completed C++ code is illustrated in Listing 1.

Listing 1 Mediator for Handling a Number of Widgets

  LspDirector* anExampleMediator = new LspDirector(555);
  anExampleMediator->ChangeQoSDetails("Expedited Forwarding");

In Listing 1, I create a mediator object that acts as the intermediary between a group of interdependent objects. The latter are sometimes called widgets to convey the fact that they are components in a broader structure. I guess an up-to-the-minute description would use the term components instead, but you get the idea! I create the widgets with an initial configuration (at construction time, not shown in Listing 1). I then change the configuration of the widgets and observe how it is reflected in the mediator.

Let’s take a very brief detour to see a simple application for the mediator.

  • + Share This
  • 🔖 Save To Your Account