Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita403c84

Browse files
committed
Add explanation for mediator pattern
1 parent86362e1 commita403c84

File tree

1 file changed

+178
-7
lines changed

1 file changed

+178
-7
lines changed

‎mediator/README.md

Lines changed: 178 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,192 @@ tags:
1111
---
1212

1313
##Intent
14-
Define an object that encapsulates how a set of objects interact.
15-
Mediator promotes loose coupling by keeping objects from referring to each
16-
other explicitly, and it lets you vary their interaction independently.
14+
15+
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling
16+
by keeping objects from referring to each other explicitly, and it lets you vary their interaction
17+
independently.
18+
19+
##Explanation
20+
21+
Real-world example
22+
23+
>Rogue, wizard, hobbit, and hunter have decided to join their forces and travel in the same
24+
>party. To avoid coupling each member with each other, they use the party interface to
25+
>communicate with each other.
26+
27+
In plain words
28+
29+
>Mediator decouples a set of classes by forcing their communications flow through a mediating
30+
>object.
31+
32+
Wikipedia says
33+
34+
>In software engineering, the mediator pattern defines an object that encapsulates how a set of
35+
>objects interact. This pattern is considered to be a behavioral pattern due to the way it can
36+
>alter the program's running behavior. In object-oriented programming, programs often consist of
37+
>many classes. Business logic and computation are distributed among these classes. However, as
38+
>more classes are added to a program, especially during maintenance and/or refactoring, the
39+
>problem of communication between these classes may become more complex. This makes the program
40+
>harder to read and maintain. Furthermore, it can become difficult to change the program, since
41+
>any change may affect code in several other classes. With the mediator pattern, communication
42+
>between objects is encapsulated within a mediator object. Objects no longer communicate directly
43+
>with each other, but instead communicate through the mediator. This reduces the dependencies
44+
>between communicating objects, thereby reducing coupling.
45+
46+
**Programmatic Example**
47+
48+
In this example, the mediator encapsulates how a set of objects interact. Instead of referring to
49+
each other directly they use the mediator interface.
50+
51+
The party members`Rogue`,`Wizard`,`Hobbit`, and`Hunter` all inherit from the`PartyMemberBase`
52+
implementing the`PartyMember` interface.
53+
54+
```java
55+
publicinterfacePartyMember {
56+
57+
voidjoinedParty(Partyparty);
58+
59+
voidpartyAction(Actionaction);
60+
61+
voidact(Actionaction);
62+
}
63+
64+
@Slf4j
65+
publicabstractclassPartyMemberBaseimplementsPartyMember {
66+
67+
protectedParty party;
68+
69+
@Override
70+
publicvoidjoinedParty(Partyparty) {
71+
LOGGER.info("{} joins the party",this);
72+
this.party= party;
73+
}
74+
75+
@Override
76+
publicvoidpartyAction(Actionaction) {
77+
LOGGER.info("{} {}",this, action.getDescription());
78+
}
79+
80+
@Override
81+
publicvoidact(Actionaction) {
82+
if (party!=null) {
83+
LOGGER.info("{} {}",this, action);
84+
party.act(this, action);
85+
}
86+
}
87+
88+
@Override
89+
publicabstractStringtoString();
90+
}
91+
92+
publicclassRogueextendsPartyMemberBase {
93+
94+
@Override
95+
publicStringtoString() {
96+
return"Rogue";
97+
}
98+
}
99+
100+
// Wizard, Hobbit, and Hunter are implemented similarly
101+
```
102+
103+
Our mediator system consists of`Party` interface and its implementation.
104+
105+
```java
106+
publicinterfaceParty {
107+
108+
voidaddMember(PartyMembermember);
109+
110+
voidact(PartyMemberactor,Actionaction);
111+
}
112+
113+
publicclassPartyImplimplementsParty {
114+
115+
privatefinalList<PartyMember> members;
116+
117+
publicPartyImpl() {
118+
members=newArrayList<>();
119+
}
120+
121+
@Override
122+
publicvoidact(PartyMemberactor,Actionaction) {
123+
for (var member: members) {
124+
if (!member.equals(actor)) {
125+
member.partyAction(action);
126+
}
127+
}
128+
}
129+
130+
@Override
131+
publicvoidaddMember(PartyMembermember) {
132+
members.add(member);
133+
member.joinedParty(this);
134+
}
135+
}
136+
```
137+
138+
Here's a demo showing the mediator pattern in action.
139+
140+
```java
141+
// create party and members
142+
Party party=newPartyImpl();
143+
var hobbit=newHobbit();
144+
var wizard=newWizard();
145+
var rogue=newRogue();
146+
var hunter=newHunter();
147+
148+
// add party members
149+
party.addMember(hobbit);
150+
party.addMember(wizard);
151+
party.addMember(rogue);
152+
party.addMember(hunter);
153+
154+
// perform actions -> the other party members
155+
// are notified by the party
156+
hobbit.act(Action.ENEMY);
157+
wizard.act(Action.TALE);
158+
rogue.act(Action.GOLD);
159+
hunter.act(Action.HUNT);
160+
```
161+
162+
Here's the console output from running the example.
163+
164+
```
165+
Hobbit joins the party
166+
Wizard joins the party
167+
Rogue joins the party
168+
Hunter joins the party
169+
Hobbit spotted enemies
170+
Wizard runs for cover
171+
Rogue runs for cover
172+
Hunter runs for cover
173+
Wizard tells a tale
174+
Hobbit comes to listen
175+
Rogue comes to listen
176+
Hunter comes to listen
177+
Rogue found gold
178+
Hobbit takes his share of the gold
179+
Wizard takes his share of the gold
180+
Hunter takes his share of the gold
181+
Hunter hunted a rabbit
182+
Hobbit arrives for dinner
183+
Wizard arrives for dinner
184+
Rogue arrives for dinner
185+
```
17186

18187
##Class diagram
188+
19189
![alt text](./etc/mediator_1.png"Mediator")
20190

21191
##Applicability
192+
22193
Use the Mediator pattern when
23194

24-
*a set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand
25-
*reusing an object is difficult because it refers to and communicates with many other objects
26-
*a behavior that's distributed between several classes should be customizable without a lot of subclassing
195+
*A set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand
196+
*Reusing an object is difficult because it refers to and communicates with many other objects
197+
*A behavior that's distributed between several classes should be customizable without a lot of subclassing
27198

28-
##Real world examples
199+
##Known uses
29200

30201
* All scheduleXXX() methods of[java.util.Timer](http://docs.oracle.com/javase/8/docs/api/java/util/Timer.html)
31202
*[java.util.concurrent.Executor#execute()](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html#execute-java.lang.Runnable-)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp