Mediator

Managing many-to-many relationships in classes can be a complicated prospect. Let's consider a form that contains a number of controls, each of which wants to know if other controls on the page are valid before performing their action. Unfortunately, having each control know about each other control creates a maintenance nightmare. Each time a new control is added, each other control needs to be modified.

A mediator will sit between the various components and act as a single place in which message routing changes can be made. By doing so the mediator simplifies the otherwise complex work needed to maintain the code. In the case of controls on a form, the mediator is likely to be the form itself. The mediator acts much like a real life mediator would, clarifying and routing information exchange between a number of parties:

Mediator

Implementation

In the land of Westeros there are many times when a mediator is needed. Frequently the mediator ends up deceased, but I'm sure that won't be the case with our example.

There are a number of great families in Westeros who own large castles and vast tracts of land. Lesser lords swear themselves to the great houses forming an alliance, frequently supported through marriage.

When coordinating the various houses sworn to them, the great lord will act as a mediator, communicating information back and forth between the lesser lords and resolving any disputes they may have amongst themselves.

In this example we'll greatly simplify the communication between the houses and say that all messages pass through the great lord. In this case we'll use the house of Stark as our great lord. They have a number of other houses which talk with them. Each of the houses looks roughly like the following:

class Karstark {
  constructor(greatLord) {
    this.greatLord = greatLord;
  }
  receiveMessage(message) {
  }
  sendMessage(message) {
    this.greatLord.routeMessage(message);
  }
}

They have two functions, one of which receives messages from a third party and one of which sends messages out to their great lord, which is set upon instantiation. The HouseStark class looks like the following:

class HouseStark {
  constructor() {
    this.karstark = new Karstark(this);
    this.bolton = new Bolton(this);
    this.frey = new Frey(this);
    this.umber = new Umber(this);
  }
  routeMessage(message) {
  }
}

By passing all messages through the HouseStark class the various other houses do not need to concern themselves with how their messages are routed. This responsibility is handed off to HouseStark which acts as the mediator.

Mediators are best used when the communication is both complex and well defined. If the communication is not complex then the mediator adds extra complexity. If the communication is ill defined then it becomes difficult to codify the communication rules in a single place.

Simplifying communication between many-to-many objects is certainly useful in JavaScript. I would actually argue that in many ways jQuery acts as a mediator. When acting on a set of items on the page, it serves to simplify communication by abstracting away code's need to know exactly which objects on the page are being changed. For instance:

$(".error").slideToggle();

Is jQuery shorthand for toggling the visibility of all the elements on the page which have the error class?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset