Browser docs



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.


Real-world example

Rogue, wizard, hobbit, and hunter have decided to join their forces and travel in the same party. To avoid coupling each member with each other, they use the party interface to communicate with each other.

In plain words

Mediator decouples a set of classes by forcing their communications flow through a mediating object.

Wikipedia says

In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program’s running behavior. In object-oriented programming, programs often consist of many classes. Business logic and computation are distributed among these classes. However, as more classes are added to a program, especially during maintenance and/or refactoring, the problem of communication between these classes may become more complex. This makes the program harder to read and maintain. Furthermore, it can become difficult to change the program, since any change may affect code in several other classes. With the mediator pattern, communication between objects is encapsulated within a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects, thereby reducing coupling.

Programmatic Example

In this example, the mediator encapsulates how a set of objects interact. Instead of referring to each other directly they use the mediator interface.

The party members Rogue, Wizard, Hobbit, and Hunter all inherit from the PartyMemberBase implementing the PartyMember interface.

 1public interface PartyMember {
 3  void joinedParty(Party party);
 5  void partyAction(Action action);
 7  void act(Action action);
11public abstract class PartyMemberBase implements PartyMember {
13  protected Party party;
15  @Override
16  public void joinedParty(Party party) {
17"{} joins the party", this);
18 = party;
19  }
21  @Override
22  public void partyAction(Action action) {
23"{} {}", this, action.getDescription());
24  }
26  @Override
27  public void act(Action action) {
28    if (party != null) {
29"{} {}", this, action);
30      party.act(this, action);
31    }
32  }
34  @Override
35  public abstract String toString();
38public class Rogue extends PartyMemberBase {
40  @Override
41  public String toString() {
42    return "Rogue";
43  }
46// Wizard, Hobbit, and Hunter are implemented similarly

Our mediator system consists of Party interface and its implementation.

 1public interface Party {
 3  void addMember(PartyMember member);
 5  void act(PartyMember actor, Action action);
 8public class PartyImpl implements Party {
10  private final List<PartyMember> members;
12  public PartyImpl() {
13    members = new ArrayList<>();
14  }
16  @Override
17  public void act(PartyMember actor, Action action) {
18    for (var member : members) {
19      if (!member.equals(actor)) {
20        member.partyAction(action);
21      }
22    }
23  }
25  @Override
26  public void addMember(PartyMember member) {
27    members.add(member);
28    member.joinedParty(this);
29  }

Here’s a demo showing the mediator pattern in action.

 1    // create party and members
 2    Party party = new PartyImpl();
 3    var hobbit = new Hobbit();
 4    var wizard = new Wizard();
 5    var rogue = new Rogue();
 6    var hunter = new Hunter();
 8    // add party members
 9    party.addMember(hobbit);
10    party.addMember(wizard);
11    party.addMember(rogue);
12    party.addMember(hunter);
14    // perform actions -> the other party members
15    // are notified by the party
16    hobbit.act(Action.ENEMY);
17    wizard.act(Action.TALE);
18    rogue.act(Action.GOLD);
19    hunter.act(Action.HUNT);

Here’s the console output from running the example.

Hobbit joins the party
Wizard joins the party
Rogue joins the party
Hunter joins the party
Hobbit spotted enemies
Wizard runs for cover
Rogue runs for cover
Hunter runs for cover
Wizard tells a tale
Hobbit comes to listen
Rogue comes to listen
Hunter comes to listen
Rogue found gold
Hobbit takes his share of the gold
Wizard takes his share of the gold
Hunter takes his share of the gold
Hunter hunted a rabbit
Hobbit arrives for dinner
Wizard arrives for dinner
Rogue arrives for dinner

Class diagram

alt text


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

Known uses