Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Whole-part business models, such as a product composed of a number of component parts

Models that represent business workflow, such as an order invoice being processed by accounting, manufacturing, and shipping services

For a real-world illustration of the Mediator pattern, consider conference calls. Many telephone companies offer teleconferencing, and you could consider the switchboard as a sort of mediator. It contains logic (presumably) to route messages between the individuals involved in the conference call. The participants send messages (talk), and the switchboard responds, directing messages to specific participants. Some callers are routed to Burma or Antwerp, while messages that start with “manager” are routed only to the manager of the conference call.

Implementation

The Mediator class diagram is shown in Figure 2.8.

Figure 2.8. Mediator class diagram

The Mediator pattern requires:

Mediator – The interface that defines the methods clients can call on a Mediator.

ConcreteMediator – The class that implements the Mediator interface. This class mediates among several client classes. It contains application-specific information about processes, and the ConcreteMediator might have some hardcoded references to its clients. Based on the information the Mediator receives, it can either invoke specific methods on the clients, or invoke a generic method to inform clients of a change or a combination of both.

Client – The interface that defines the general methods a Mediator can use to inform client instances.

ConcreteClient – A class that implements the Client interface and provides an implementation to each of the client methods. The ConcreteClient can keep a reference to a Mediator instance to inform colleague clients of a change (through the Mediator).

Benefits and Drawbacks

The Mediator has three advantages:

The individual components become simpler and easier to deal with, since they no longer need to directly pass messages to each other. Components are more generic, since they no longer need to contain logic to deal with their communication with other components. This application-specific information is contained in the Mediator.

The overall communications strategy becomes easier to maintain as well, since it is now the exclusive responsibility of the mediator.

58