Skip to content

Commit ca59792

Browse files
committed
Added event aggregator pattern.
1 parent c5a0226 commit ca59792

File tree

15 files changed

+389
-1
lines changed

15 files changed

+389
-1
lines changed

event-aggregator/etc/classes.png

44.6 KB
Loading

event-aggregator/etc/classes.ucls

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
4+
<class id="1" language="java" name="com.iluwatar.KingsHand" project="event-aggregator"
5+
file="/event-aggregator/src/main/java/com/iluwatar/KingsHand.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="805" y="431"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<interface id="2" language="java" name="com.iluwatar.EventObserver" project="event-aggregator"
14+
file="/event-aggregator/src/main/java/com/iluwatar/EventObserver.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="784" y="586"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</interface>
22+
<enumeration id="3" language="java" name="com.iluwatar.Weekday" project="event-aggregator"
23+
file="/event-aggregator/src/main/java/com/iluwatar/Weekday.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="437" y="195"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</enumeration>
31+
<class id="4" language="java" name="com.iluwatar.KingJoffrey" project="event-aggregator"
32+
file="/event-aggregator/src/main/java/com/iluwatar/KingJoffrey.java" binary="false" corner="BOTTOM_RIGHT">
33+
<position height="-1" width="-1" x="380" y="413"/>
34+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
35+
sort-features="false" accessors="true" visibility="true">
36+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
37+
<operations public="true" package="true" protected="true" private="true" static="true"/>
38+
</display>
39+
</class>
40+
<class id="5" language="java" name="com.iluwatar.LordVarys" project="event-aggregator"
41+
file="/event-aggregator/src/main/java/com/iluwatar/LordVarys.java" binary="false" corner="BOTTOM_RIGHT">
42+
<position height="-1" width="-1" x="581" y="422"/>
43+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
44+
sort-features="false" accessors="true" visibility="true">
45+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
46+
<operations public="true" package="true" protected="true" private="true" static="true"/>
47+
</display>
48+
</class>
49+
<class id="6" language="java" name="com.iluwatar.LordBaelish" project="event-aggregator"
50+
file="/event-aggregator/src/main/java/com/iluwatar/LordBaelish.java" binary="false" corner="BOTTOM_RIGHT">
51+
<position height="-1" width="-1" x="1050" y="422"/>
52+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
53+
sort-features="false" accessors="true" visibility="true">
54+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
55+
<operations public="true" package="true" protected="true" private="true" static="true"/>
56+
</display>
57+
</class>
58+
<class id="7" language="java" name="com.iluwatar.EventEmitter" project="event-aggregator"
59+
file="/event-aggregator/src/main/java/com/iluwatar/EventEmitter.java" binary="false" corner="BOTTOM_RIGHT">
60+
<position height="-1" width="-1" x="1036" y="149"/>
61+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
62+
sort-features="false" accessors="true" visibility="true">
63+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
64+
<operations public="true" package="true" protected="true" private="true" static="true"/>
65+
</display>
66+
</class>
67+
<class id="8" language="java" name="com.iluwatar.Scout" project="event-aggregator"
68+
file="/event-aggregator/src/main/java/com/iluwatar/Scout.java" binary="false" corner="BOTTOM_RIGHT">
69+
<position height="-1" width="-1" x="1275" y="422"/>
70+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
71+
sort-features="false" accessors="true" visibility="true">
72+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
73+
<operations public="true" package="true" protected="true" private="true" static="true"/>
74+
</display>
75+
</class>
76+
<enumeration id="9" language="java" name="com.iluwatar.Event" project="event-aggregator"
77+
file="/event-aggregator/src/main/java/com/iluwatar/Event.java" binary="false" corner="BOTTOM_RIGHT">
78+
<position height="-1" width="-1" x="721" y="159"/>
79+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
80+
sort-features="false" accessors="true" visibility="true">
81+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
82+
<operations public="true" package="true" protected="true" private="true" static="true"/>
83+
</display>
84+
</enumeration>
85+
<realization id="10">
86+
<end type="SOURCE" refId="1"/>
87+
<end type="TARGET" refId="2"/>
88+
</realization>
89+
<association id="11">
90+
<bendpoint x="927" y="360"/>
91+
<end type="SOURCE" refId="7" navigable="false">
92+
<attribute id="12" name="observers"/>
93+
<multiplicity id="13" minimum="0" maximum="2147483647"/>
94+
</end>
95+
<end type="TARGET" refId="2" navigable="true"/>
96+
<display labels="true" multiplicity="true"/>
97+
</association>
98+
<generalization id="14">
99+
<end type="SOURCE" refId="1"/>
100+
<end type="TARGET" refId="7"/>
101+
</generalization>
102+
<generalization id="15">
103+
<end type="SOURCE" refId="6"/>
104+
<end type="TARGET" refId="7"/>
105+
</generalization>
106+
<realization id="16">
107+
<end type="SOURCE" refId="4"/>
108+
<end type="TARGET" refId="2"/>
109+
</realization>
110+
<generalization id="17">
111+
<end type="SOURCE" refId="8"/>
112+
<end type="TARGET" refId="7"/>
113+
</generalization>
114+
<generalization id="18">
115+
<end type="SOURCE" refId="5"/>
116+
<end type="TARGET" refId="7"/>
117+
</generalization>
118+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
119+
sort-features="false" accessors="true" visibility="true">
120+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
121+
<operations public="true" package="true" protected="true" private="true" static="true"/>
122+
</classifier-display>
123+
<association-display labels="true" multiplicity="true"/>
124+
</class-diagram>

event-aggregator/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.iluwatar</groupId>
6+
<artifactId>java-design-patterns</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>event-aggregator</artifactId>
10+
<dependencies>
11+
<dependency>
12+
<groupId>junit</groupId>
13+
<artifactId>junit</artifactId>
14+
<scope>test</scope>
15+
</dependency>
16+
</dependencies>
17+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.iluwatar;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
*
8+
* The Event Aggregator pattern channels events from multiple objects
9+
* into a single object to simplify registration for clients.
10+
*
11+
* In the example LordBaelish, LordVarys and Scout deliver events to
12+
* KingsHand. KingsHand, the event aggregator, then delivers the events
13+
* to KingJoffrey.
14+
*
15+
*/
16+
public class App {
17+
18+
public static void main(String[] args) {
19+
20+
KingJoffrey kingJoffrey = new KingJoffrey();
21+
KingsHand kingsHand = new KingsHand(kingJoffrey);
22+
23+
List<EventEmitter> emitters = new ArrayList<>();
24+
emitters.add(kingsHand);
25+
emitters.add(new LordBaelish(kingsHand));
26+
emitters.add(new LordVarys(kingsHand));
27+
emitters.add(new Scout(kingsHand));
28+
29+
for (Weekday day: Weekday.values()) {
30+
for (EventEmitter emitter: emitters) {
31+
emitter.timePasses(day);
32+
}
33+
}
34+
}
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* Event enumeration.
6+
*
7+
*/
8+
public enum Event {
9+
10+
STARK_SIGHTED("Stark sighted"), WARSHIPS_APPROACHING("Warships approaching"), TRAITOR_DETECTED("Traitor detected");
11+
12+
private String description;
13+
14+
Event(String description) {
15+
this.description = description;
16+
}
17+
18+
public String toString() {
19+
return description;
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.iluwatar;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
*
8+
* EventEmitter is the base class for event producers that can be observed.
9+
*
10+
*/
11+
public abstract class EventEmitter {
12+
13+
private List<EventObserver> observers;
14+
15+
public EventEmitter() {
16+
observers = new LinkedList<>();
17+
}
18+
19+
public EventEmitter(EventObserver obs) {
20+
this();
21+
registerObserver(obs);
22+
}
23+
24+
public void registerObserver(EventObserver obs) {
25+
observers.add(obs);
26+
}
27+
28+
protected void notifyObservers(Event e) {
29+
for (EventObserver obs: observers) {
30+
obs.onEvent(e);
31+
}
32+
}
33+
34+
public abstract void timePasses(Weekday day);
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* Observers of events implement this interface.
6+
*
7+
*/
8+
public interface EventObserver {
9+
10+
void onEvent(Event e);
11+
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* KingJoffrey observes events from KingsHand.
6+
*
7+
*/
8+
public class KingJoffrey implements EventObserver {
9+
10+
@Override
11+
public void onEvent(Event e) {
12+
System.out.println("Received event from the King's Hand: " + e.toString());
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* KingsHand observes events from multiple sources and delivers them
6+
* to listeners.
7+
*
8+
*/
9+
public class KingsHand extends EventEmitter implements EventObserver {
10+
11+
public KingsHand() {
12+
super();
13+
}
14+
15+
public KingsHand(EventObserver obs) {
16+
super(obs);
17+
}
18+
19+
@Override
20+
public void onEvent(Event e) {
21+
notifyObservers(e);
22+
}
23+
24+
@Override
25+
public void timePasses(Weekday day) {
26+
// NOP
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* LordBaelish produces events.
6+
*
7+
*/
8+
public class LordBaelish extends EventEmitter {
9+
10+
public LordBaelish() {
11+
super();
12+
}
13+
14+
public LordBaelish(EventObserver obs) {
15+
super(obs);
16+
}
17+
18+
@Override
19+
public void timePasses(Weekday day) {
20+
if (day.equals(Weekday.FRIDAY)) {
21+
notifyObservers(Event.STARK_SIGHTED);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)