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 {
2
3 void joinedParty(Party party);
4
5 void partyAction(Action action);
6
7 void act(Action action);
8}
9
10@Slf4j
11public abstract class PartyMemberBase implements PartyMember {
12
13 protected Party party;
14
15 @Override
16 public void joinedParty(Party party) {
17 LOGGER.info("{} joins the party", this);
18 this.party = party;
19 }
20
21 @Override
22 public void partyAction(Action action) {
23 LOGGER.info("{} {}", this, action.getDescription());
24 }
25
26 @Override
27 public void act(Action action) {
28 if (party != null) {
29 LOGGER.info("{} {}", this, action);
30 party.act(this, action);
31 }
32 }
33
34 @Override
35 public abstract String toString();
36}
37
38public class Rogue extends PartyMemberBase {
39
40 @Override
41 public String toString() {
42 return "Rogue";
43 }
44}
45
46// Wizard, Hobbit, and Hunter are implemented similarly
Our mediator system consists of Party
interface and its implementation.
1public interface Party {
2
3 void addMember(PartyMember member);
4
5 void act(PartyMember actor, Action action);
6}
7
8public class PartyImpl implements Party {
9
10 private final List<PartyMember> members;
11
12 public PartyImpl() {
13 members = new ArrayList<>();
14 }
15
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 }
24
25 @Override
26 public void addMember(PartyMember member) {
27 members.add(member);
28 member.joinedParty(this);
29 }
30}
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();
7
8 // add party members
9 party.addMember(hobbit);
10 party.addMember(wizard);
11 party.addMember(rogue);
12 party.addMember(hunter);
13
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
Use the Mediator pattern when