Added event aggregator pattern.
This commit is contained in:
parent
c5a02266c8
commit
ca59792916
BIN
event-aggregator/etc/classes.png
Normal file
BIN
event-aggregator/etc/classes.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 45 KiB |
124
event-aggregator/etc/classes.ucls
Normal file
124
event-aggregator/etc/classes.ucls
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||||
|
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||||
|
<class id="1" language="java" name="com.iluwatar.KingsHand" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/KingsHand.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="805" y="431"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<interface id="2" language="java" name="com.iluwatar.EventObserver" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/EventObserver.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="784" y="586"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</interface>
|
||||||
|
<enumeration id="3" language="java" name="com.iluwatar.Weekday" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/Weekday.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="437" y="195"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</enumeration>
|
||||||
|
<class id="4" language="java" name="com.iluwatar.KingJoffrey" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/KingJoffrey.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="380" y="413"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<class id="5" language="java" name="com.iluwatar.LordVarys" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/LordVarys.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="581" y="422"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<class id="6" language="java" name="com.iluwatar.LordBaelish" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/LordBaelish.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="1050" y="422"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<class id="7" language="java" name="com.iluwatar.EventEmitter" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/EventEmitter.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="1036" y="149"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<class id="8" language="java" name="com.iluwatar.Scout" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/Scout.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="1275" y="422"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<enumeration id="9" language="java" name="com.iluwatar.Event" project="event-aggregator"
|
||||||
|
file="/event-aggregator/src/main/java/com/iluwatar/Event.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="721" y="159"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</enumeration>
|
||||||
|
<realization id="10">
|
||||||
|
<end type="SOURCE" refId="1"/>
|
||||||
|
<end type="TARGET" refId="2"/>
|
||||||
|
</realization>
|
||||||
|
<association id="11">
|
||||||
|
<bendpoint x="927" y="360"/>
|
||||||
|
<end type="SOURCE" refId="7" navigable="false">
|
||||||
|
<attribute id="12" name="observers"/>
|
||||||
|
<multiplicity id="13" minimum="0" maximum="2147483647"/>
|
||||||
|
</end>
|
||||||
|
<end type="TARGET" refId="2" navigable="true"/>
|
||||||
|
<display labels="true" multiplicity="true"/>
|
||||||
|
</association>
|
||||||
|
<generalization id="14">
|
||||||
|
<end type="SOURCE" refId="1"/>
|
||||||
|
<end type="TARGET" refId="7"/>
|
||||||
|
</generalization>
|
||||||
|
<generalization id="15">
|
||||||
|
<end type="SOURCE" refId="6"/>
|
||||||
|
<end type="TARGET" refId="7"/>
|
||||||
|
</generalization>
|
||||||
|
<realization id="16">
|
||||||
|
<end type="SOURCE" refId="4"/>
|
||||||
|
<end type="TARGET" refId="2"/>
|
||||||
|
</realization>
|
||||||
|
<generalization id="17">
|
||||||
|
<end type="SOURCE" refId="8"/>
|
||||||
|
<end type="TARGET" refId="7"/>
|
||||||
|
</generalization>
|
||||||
|
<generalization id="18">
|
||||||
|
<end type="SOURCE" refId="5"/>
|
||||||
|
<end type="TARGET" refId="7"/>
|
||||||
|
</generalization>
|
||||||
|
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</classifier-display>
|
||||||
|
<association-display labels="true" multiplicity="true"/>
|
||||||
|
</class-diagram>
|
17
event-aggregator/pom.xml
Normal file
17
event-aggregator/pom.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>event-aggregator</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
35
event-aggregator/src/main/java/com/iluwatar/App.java
Normal file
35
event-aggregator/src/main/java/com/iluwatar/App.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* The Event Aggregator pattern channels events from multiple objects
|
||||||
|
* into a single object to simplify registration for clients.
|
||||||
|
*
|
||||||
|
* In the example LordBaelish, LordVarys and Scout deliver events to
|
||||||
|
* KingsHand. KingsHand, the event aggregator, then delivers the events
|
||||||
|
* to KingJoffrey.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
KingJoffrey kingJoffrey = new KingJoffrey();
|
||||||
|
KingsHand kingsHand = new KingsHand(kingJoffrey);
|
||||||
|
|
||||||
|
List<EventEmitter> emitters = new ArrayList<>();
|
||||||
|
emitters.add(kingsHand);
|
||||||
|
emitters.add(new LordBaelish(kingsHand));
|
||||||
|
emitters.add(new LordVarys(kingsHand));
|
||||||
|
emitters.add(new Scout(kingsHand));
|
||||||
|
|
||||||
|
for (Weekday day: Weekday.values()) {
|
||||||
|
for (EventEmitter emitter: emitters) {
|
||||||
|
emitter.timePasses(day);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
event-aggregator/src/main/java/com/iluwatar/Event.java
Normal file
21
event-aggregator/src/main/java/com/iluwatar/Event.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Event enumeration.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public enum Event {
|
||||||
|
|
||||||
|
STARK_SIGHTED("Stark sighted"), WARSHIPS_APPROACHING("Warships approaching"), TRAITOR_DETECTED("Traitor detected");
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
Event(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* EventEmitter is the base class for event producers that can be observed.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public abstract class EventEmitter {
|
||||||
|
|
||||||
|
private List<EventObserver> observers;
|
||||||
|
|
||||||
|
public EventEmitter() {
|
||||||
|
observers = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public EventEmitter(EventObserver obs) {
|
||||||
|
this();
|
||||||
|
registerObserver(obs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerObserver(EventObserver obs) {
|
||||||
|
observers.add(obs);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void notifyObservers(Event e) {
|
||||||
|
for (EventObserver obs: observers) {
|
||||||
|
obs.onEvent(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void timePasses(Weekday day);
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Observers of events implement this interface.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface EventObserver {
|
||||||
|
|
||||||
|
void onEvent(Event e);
|
||||||
|
|
||||||
|
}
|
14
event-aggregator/src/main/java/com/iluwatar/KingJoffrey.java
Normal file
14
event-aggregator/src/main/java/com/iluwatar/KingJoffrey.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* KingJoffrey observes events from KingsHand.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class KingJoffrey implements EventObserver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEvent(Event e) {
|
||||||
|
System.out.println("Received event from the King's Hand: " + e.toString());
|
||||||
|
}
|
||||||
|
}
|
28
event-aggregator/src/main/java/com/iluwatar/KingsHand.java
Normal file
28
event-aggregator/src/main/java/com/iluwatar/KingsHand.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* KingsHand observes events from multiple sources and delivers them
|
||||||
|
* to listeners.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class KingsHand extends EventEmitter implements EventObserver {
|
||||||
|
|
||||||
|
public KingsHand() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public KingsHand(EventObserver obs) {
|
||||||
|
super(obs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEvent(Event e) {
|
||||||
|
notifyObservers(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void timePasses(Weekday day) {
|
||||||
|
// NOP
|
||||||
|
}
|
||||||
|
}
|
24
event-aggregator/src/main/java/com/iluwatar/LordBaelish.java
Normal file
24
event-aggregator/src/main/java/com/iluwatar/LordBaelish.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LordBaelish produces events.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LordBaelish extends EventEmitter {
|
||||||
|
|
||||||
|
public LordBaelish() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LordBaelish(EventObserver obs) {
|
||||||
|
super(obs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void timePasses(Weekday day) {
|
||||||
|
if (day.equals(Weekday.FRIDAY)) {
|
||||||
|
notifyObservers(Event.STARK_SIGHTED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
event-aggregator/src/main/java/com/iluwatar/LordVarys.java
Normal file
24
event-aggregator/src/main/java/com/iluwatar/LordVarys.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* LordVarys produces events.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LordVarys extends EventEmitter {
|
||||||
|
|
||||||
|
public LordVarys() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LordVarys(EventObserver obs) {
|
||||||
|
super(obs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void timePasses(Weekday day) {
|
||||||
|
if (day.equals(Weekday.SATURDAY)) {
|
||||||
|
notifyObservers(Event.TRAITOR_DETECTED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
event-aggregator/src/main/java/com/iluwatar/Scout.java
Normal file
24
event-aggregator/src/main/java/com/iluwatar/Scout.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Scout produces events.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Scout extends EventEmitter {
|
||||||
|
|
||||||
|
public Scout() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scout(EventObserver obs) {
|
||||||
|
super(obs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void timePasses(Weekday day) {
|
||||||
|
if (day.equals(Weekday.TUESDAY)) {
|
||||||
|
notifyObservers(Event.WARSHIPS_APPROACHING);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
event-aggregator/src/main/java/com/iluwatar/Weekday.java
Normal file
16
event-aggregator/src/main/java/com/iluwatar/Weekday.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public enum Weekday {
|
||||||
|
|
||||||
|
MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"), SUNDAY("Sunday");
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
Weekday(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
}
|
13
event-aggregator/src/test/java/com/iluwatar/AppTest.java
Normal file
13
event-aggregator/src/test/java/com/iluwatar/AppTest.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.iluwatar.App;
|
||||||
|
|
||||||
|
public class AppTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
|
}
|
||||||
|
}
|
3
pom.xml
3
pom.xml
@ -38,7 +38,8 @@
|
|||||||
<module>double-checked-locking</module>
|
<module>double-checked-locking</module>
|
||||||
<module>servant</module>
|
<module>servant</module>
|
||||||
<module>service-locator</module>
|
<module>service-locator</module>
|
||||||
<module>null-object</module>
|
<module>null-object</module>
|
||||||
|
<module>event-aggregator</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user