17
The Mediator Pattern (Behavioral) ©SoftMoore Consulting Slide 1

The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Embed Size (px)

Citation preview

Page 1: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

The Mediator Pattern(Behavioral)

©SoftMoore Consulting Slide 1

Page 2: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Mediator in the Real World

©SoftMoore Consulting Slide 2

Page 3: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Motivation

• Consider a situation where many objects need to communicate with each other. In the worst case, every object needs to know about every other object. The lines of communication can become overly complex and can inhibit reuse of the classes outside of the application context.

• In some cases communication between the objects can be simplified by introducing an intermediate “mediator” object. Communication between the various application objects goes through the mediator.

• The mediator takes on the role of a hub or router and facilitates communication between the application classes.

©SoftMoore Consulting Slide 3

Page 4: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Motivation(continued)

©SoftMoore Consulting Slide 4

Without a Mediator With a Mediator

O1 O2

O1

O1

O3

O1 O1

O1

O1

O1M

10 possible pathsof communication

5 possible pathsof communication

Page 5: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Motivation(continued)

• Consider the implementation of a dialog box as part of a graphical user interface. A dialog box can contain a collection of widgets such as buttons, text fields, menus, list boxes, etc., with dependencies between the widgets.

• The individual widgets should be reusable in different dialog boxes, so they must be designed without knowledge of each other.

• One approach would be to create application-specific subclasses of the widgets with knowledge of the other widgets in the dialog box.

©SoftMoore Consulting Slide 5

Page 6: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Motivation(continued)

• A more general approach is to create mediator that is responsible for controlling and coordinating the interactions of the widgets in the dialog box. Often, the dialog box itself can be used as the mediator.

©SoftMoore Consulting Slide 6

Page 7: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Mediator Pattern

• Intent: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

• Applicability: Use the Mediator pattern when– a set of objects communicate in well-defined but complex ways.

The resulting interdependencies are unstructured and difficult to understand.

– reusing an object is difficult because it refers to and communicates with many other objects.

– a behavior that’s distributed between several classes should be customizable without a lot of subclassing.

Slide 7©SoftMoore Consulting

Page 8: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Mediator Pattern(continued)

©SoftMoore Consulting Slide 8

Mediator Colleague

ConcreteMediator ConcreteColleague1 ConcreteColleague2

Structure

Page 9: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Mediator Pattern(continued)

©SoftMoore Consulting Slide 9

: ConcreteMediator

Typical Object Diagram

: Colleague

mediator

: Colleague

mediator

: Colleague

mediator

: Colleague

mediator

: Colleague

mediator

Page 10: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

©SoftMoore Consulting

Mediator Pattern(continued)

Participants

• Mediator– defines an interface for communicating with Colleague objects.

• ConcreteMediator– implements cooperative behavior by coordinating Colleague

objects.– knows and maintains its colleagues.

• Colleague classes– each colleague knows its mediator object.– each colleague communicates with its mediator whenever it

would have otherwise communicated with another colleague.

Slide 10

Page 11: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Mediator Pattern(continued)

Collaborations

• Colleagues send and receive requests from a Mediator object.

• The mediator implements the cooperative behavior by routing requests between appropriate colleagues.

©SoftMoore Consulting Slide 11

Page 12: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Mediator Pattern(continued)

Consequences

• It limits subclassing. A mediator localizes behavior that otherwise would be distributed among several objects. Changing this behavior requires subclassing Mediator only; Colleague classes can be reused as is.

• It decouples colleagues. A mediator promotes loose coupling between colleagues. You can vary and reuse Colleague and Mediator classes independently.

• It simplifies object protocols. A mediator replaces many-to-many interactions with one-to-many interactions between the mediator and its colleagues. One-to-many relationships are easier to understand, maintain, and extend.

©SoftMoore Consulting Slide 12

Page 13: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

Mediator Pattern(continued)

Consequences (continued)

• It abstracts how objects cooperate. Making mediation an independent concept and encapsulating it in an object lets you focus on how objects interact apart from their individual behavior. That can help clarify how objects interact in a system.

• It centralized control. The Mediator pattern trades complexity of interaction for complexity in the mediator. Because a mediator encapsulates protocols, it can become more complex than any individual colleague. This can make the mediator itself a monolith that is hard to maintain.

©SoftMoore Consulting Slide 13

Page 14: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

©SoftMoore Consulting

Mediator Pattern(continued)

Implementation

• There is no need to define an abstract mediator class when colleagues work with only one mediator. The abstract coupling that the Mediator class provides lets colleagues work with different Mediator subclasses, and vice-versa.

• Colleagues have to communicate with their mediator when an event of interest occurs.– One approach is to implement Mediator as an Observer, where

Colleagues notify the mediator whenever they change state and mediator propagates the change to other colleagues.

– Another approach is to define a specialize communication interface in Mediator.

Slide 14

Page 15: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

©SoftMoore Consulting

Mediator Pattern in Java

• Most systems that facilitate asynchronous messaging implement some form of message bus as a mediator.

• The Java Message Service (JMS) is a messaging API that allows application components to create, send, receive, and read messages. It enables distributed communication that is loosely coupled, reliable, and asynchronous.

• JMS implementations– IBM MQ is a commercial implementation– Apache ActiveMQ is an open source implementation

Slide 15

Page 16: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

©SoftMoore Consulting

Related Patterns

• Façade differs from Mediator in that it abstracts a subsystem of objects to provide a more convenient interface. Its protocol is unidirectional; that is, Façade objects make requests of subsystem classes, but not vice versa. In contrast, Mediator enables cooperative behavior, and the protocol is multidirectional.

• Colleagues can communicate with the mediator using the Observer pattern.(Note: JMS allows objects to communicate using this pattern.)

Slide 16

Page 17: The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1

©SoftMoore Consulting

References

• Mediator pattern (Wikipedia)http://en.wikipedia.org/wiki/Mediator_pattern

• Mediator Pattern (Object-Oriented Design)http://www.oodesign.com/mediator-pattern.html

• Mediator Design Pattern (SourceMaking)http://sourcemaking.com/design_patterns/mediator

• Open Message Queuehttp://mq.java.net

Slide 17