Skip to content

Commit afb8973

Browse files
committed
Event driven architecture refactored.
1. Renamed Message to Event and Event to AbstractEvent 2. Generified Event and Handler 3. Updated EventDispatcher to make unsafe configuration impossible 4. Updated UML diagram accordingly
1 parent 35d6a54 commit afb8973

File tree

13 files changed

+102
-118
lines changed

13 files changed

+102
-118
lines changed

event-driven-architecture/etc/eda.png

-21.3 KB
Loading

event-driven-architecture/etc/eda.ucls

Lines changed: 60 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<class id="1" language="java" name="com.iluwatar.eda.model.User" project="event-driven-architecture"
55
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/model/User.java" binary="false"
66
corner="BOTTOM_RIGHT">
7-
<position height="-1" width="-1" x="937" y="618"/>
7+
<position height="-1" width="-1" x="437" y="535"/>
88
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
99
sort-features="false" accessors="true" visibility="true">
1010
<attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -15,7 +15,7 @@
1515
project="event-driven-architecture"
1616
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserUpdatedEventHandler.java" binary="false"
1717
corner="BOTTOM_RIGHT">
18-
<position height="-1" width="-1" x="936" y="90"/>
18+
<position height="-1" width="-1" x="763" y="379"/>
1919
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
2020
sort-features="false" accessors="true" visibility="true">
2121
<attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -26,17 +26,17 @@
2626
project="event-driven-architecture"
2727
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserCreatedEventHandler.java" binary="false"
2828
corner="BOTTOM_RIGHT">
29-
<position height="101" width="200" x="41" y="37"/>
29+
<position height="-1" width="-1" x="596" y="272"/>
3030
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
3131
sort-features="false" accessors="true" visibility="true">
3232
<attributes public="true" package="true" protected="true" private="true" static="true"/>
3333
<operations public="true" package="true" protected="true" private="true" static="true"/>
3434
</display>
3535
</class>
36-
<interface id="4" language="java" name="com.iluwatar.eda.framework.Message" project="event-driven-architecture"
37-
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Message.java" binary="false"
36+
<interface id="4" language="java" name="com.iluwatar.eda.framework.Event" project="event-driven-architecture"
37+
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Event.java" binary="false"
3838
corner="BOTTOM_RIGHT">
39-
<position height="-1" width="-1" x="298" y="189"/>
39+
<position height="-1" width="-1" x="188" y="121"/>
4040
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
4141
sort-features="false" accessors="true" visibility="true">
4242
<attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -46,7 +46,7 @@
4646
<interface id="5" language="java" name="com.iluwatar.eda.framework.Handler" project="event-driven-architecture"
4747
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Handler.java" binary="false"
4848
corner="BOTTOM_RIGHT">
49-
<position height="-1" width="-1" x="532" y="89"/>
49+
<position height="-1" width="-1" x="755" y="58"/>
5050
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
5151
sort-features="false" accessors="true" visibility="true">
5252
<attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -56,7 +56,7 @@
5656
<class id="6" language="java" name="com.iluwatar.eda.framework.EventDispatcher" project="event-driven-architecture"
5757
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/EventDispatcher.java" binary="false"
5858
corner="BOTTOM_RIGHT">
59-
<position height="-1" width="-1" x="613" y="292"/>
59+
<position height="-1" width="-1" x="499" y="122"/>
6060
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
6161
sort-features="false" accessors="true" visibility="true">
6262
<attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -66,7 +66,7 @@
6666
<class id="7" language="java" name="com.iluwatar.eda.event.UserCreatedEvent" project="event-driven-architecture"
6767
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserCreatedEvent.java" binary="false"
6868
corner="BOTTOM_RIGHT">
69-
<position height="-1" width="-1" x="145" y="618"/>
69+
<position height="-1" width="-1" x="102" y="380"/>
7070
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
7171
sort-features="false" accessors="true" visibility="true">
7272
<attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -76,117 +76,105 @@
7676
<class id="8" language="java" name="com.iluwatar.eda.event.UserUpdatedEvent" project="event-driven-architecture"
7777
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserUpdatedEvent.java" binary="false"
7878
corner="BOTTOM_RIGHT">
79-
<position height="-1" width="-1" x="769" y="455"/>
79+
<position height="-1" width="-1" x="320" y="382"/>
8080
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8181
sort-features="false" accessors="true" visibility="true">
8282
<attributes public="true" package="true" protected="true" private="true" static="true"/>
8383
<operations public="true" package="true" protected="true" private="true" static="true"/>
8484
</display>
8585
</class>
86-
<class id="9" language="java" name="com.iluwatar.eda.event.Event" project="event-driven-architecture"
87-
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/event/Event.java" binary="false"
86+
<class id="9" language="java" name="com.iluwatar.eda.event.AbstractEvent" project="event-driven-architecture"
87+
file="/event-driven-architecture/src/main/java/com/iluwatar/eda/event/AbstractEvent.java" binary="false"
8888
corner="BOTTOM_RIGHT">
89-
<position height="-1" width="-1" x="299" y="460"/>
89+
<position height="-1" width="-1" x="188" y="240"/>
9090
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
9191
sort-features="false" accessors="true" visibility="true">
9292
<attributes public="true" package="true" protected="true" private="true" static="true"/>
9393
<operations public="true" package="true" protected="true" private="true" static="true"/>
9494
</display>
9595
</class>
9696
<dependency id="10">
97-
<bendpoint x="869" y="253"/>
98-
<end type="SOURCE" refId="2"/>
99-
<end type="TARGET" refId="8"/>
100-
</dependency>
101-
<dependency id="11">
102-
<bendpoint x="140" y="196"/>
103-
<end type="SOURCE" refId="3"/>
104-
<end type="TARGET" refId="9"/>
105-
</dependency>
106-
<dependency id="12">
107-
<bendpoint x="17" y="90"/>
108-
<bendpoint x="17" y="708"/>
109-
<bendpoint x="939" y="703"/>
11097
<end type="SOURCE" refId="3"/>
11198
<end type="TARGET" refId="1"/>
11299
</dependency>
113-
<dependency id="13">
114-
<bendpoint x="935" y="318"/>
115-
<bendpoint x="936" y="417"/>
100+
<dependency id="11">
116101
<end type="SOURCE" refId="2"/>
117102
<end type="TARGET" refId="1"/>
118103
</dependency>
119-
<dependency id="14">
120-
<bendpoint x="757" y="378"/>
104+
<dependency id="12">
121105
<end type="SOURCE" refId="2"/>
122-
<end type="TARGET" refId="9"/>
106+
<end type="TARGET" refId="8"/>
123107
</dependency>
124-
<generalization id="15">
108+
<realization id="13">
109+
<end type="SOURCE" refId="2"/>
110+
<end type="TARGET" refId="5"/>
111+
</realization>
112+
<generalization id="14">
125113
<end type="SOURCE" refId="7"/>
126114
<end type="TARGET" refId="9"/>
127115
</generalization>
116+
<realization id="15">
117+
<end type="SOURCE" refId="3"/>
118+
<end type="TARGET" refId="5"/>
119+
</realization>
128120
<association id="16">
129-
<end type="SOURCE" refId="8" navigable="false">
130-
<attribute id="17" name="user"/>
131-
<multiplicity id="18" minimum="0" maximum="1"/>
121+
<end type="SOURCE" refId="6" navigable="false">
122+
<attribute id="17" name="handlers">
123+
<position height="19" width="56" x="672" y="144"/>
124+
</attribute>
125+
<multiplicity id="18" minimum="0" maximum="2147483647">
126+
<position height="17" width="23" x="797" y="142"/>
127+
</multiplicity>
132128
</end>
133-
<end type="TARGET" refId="1" navigable="true"/>
129+
<end type="TARGET" refId="5" navigable="true"/>
134130
<display labels="true" multiplicity="true"/>
135131
</association>
136132
<realization id="19">
137-
<end type="SOURCE" refId="2"/>
138-
<end type="TARGET" refId="5"/>
133+
<end type="SOURCE" refId="9"/>
134+
<end type="TARGET" refId="4"/>
139135
</realization>
140136
<dependency id="20">
141-
<end type="SOURCE" refId="5"/>
142-
<end type="TARGET" refId="9"/>
137+
<end type="SOURCE" refId="6"/>
138+
<end type="TARGET" refId="4"/>
143139
</dependency>
144140
<dependency id="21">
145-
<bendpoint x="301" y="114"/>
141+
<bendpoint x="187" y="57"/>
146142
<end type="SOURCE" refId="5"/>
147143
<end type="TARGET" refId="4"/>
148144
</dependency>
149-
<realization id="22">
150-
<end type="SOURCE" refId="9"/>
151-
<end type="TARGET" refId="4"/>
152-
</realization>
153-
<dependency id="23">
154-
<end type="SOURCE" refId="6"/>
155-
<end type="TARGET" refId="9"/>
156-
</dependency>
157-
<generalization id="24">
158-
<end type="SOURCE" refId="8"/>
159-
<end type="TARGET" refId="9"/>
160-
</generalization>
161-
<dependency id="25">
162-
<bendpoint x="141" y="250"/>
163-
<end type="SOURCE" refId="3"/>
164-
<end type="TARGET" refId="7"/>
165-
</dependency>
166-
<association id="26">
167-
<end type="SOURCE" refId="6" navigable="false">
168-
<attribute id="27" name="handlers">
169-
<position height="19" width="56" x="449" y="175"/>
145+
<association id="22">
146+
<end type="SOURCE" refId="8" navigable="false">
147+
<attribute id="23" name="user">
148+
<position height="0" width="0" x="-500" y="-83"/>
170149
</attribute>
171-
<multiplicity id="28" minimum="0" maximum="2147483647">
172-
<position height="17" width="23" x="574" y="173"/>
150+
<multiplicity id="24" minimum="0" maximum="1">
151+
<position height="0" width="0" x="-500" y="-83"/>
173152
</multiplicity>
174153
</end>
175-
<end type="TARGET" refId="5" navigable="true"/>
154+
<end type="TARGET" refId="1" navigable="true"/>
176155
<display labels="true" multiplicity="true"/>
177156
</association>
178-
<association id="29">
157+
<generalization id="25">
158+
<end type="SOURCE" refId="8"/>
159+
<end type="TARGET" refId="9"/>
160+
</generalization>
161+
<association id="26">
179162
<end type="SOURCE" refId="7" navigable="false">
180-
<attribute id="30" name="user"/>
181-
<multiplicity id="31" minimum="0" maximum="1"/>
163+
<attribute id="27" name="user">
164+
<position height="0" width="0" x="-500" y="-83"/>
165+
</attribute>
166+
<multiplicity id="28" minimum="0" maximum="1">
167+
<position height="0" width="0" x="-500" y="-83"/>
168+
</multiplicity>
182169
</end>
183170
<end type="TARGET" refId="1" navigable="true"/>
184171
<display labels="true" multiplicity="true"/>
185172
</association>
186-
<realization id="32">
173+
<dependency id="29">
174+
<bendpoint x="308" y="303"/>
187175
<end type="SOURCE" refId="3"/>
188-
<end type="TARGET" refId="5"/>
189-
</realization>
176+
<end type="TARGET" refId="7"/>
177+
</dependency>
190178
<classifier-display autosize="true" stereotype="true" package="true" initial-value="true" signature="true"
191179
sort-features="false" accessors="true" visibility="true">
192180
<attributes public="true" package="true" protected="true" private="true" static="true"/>

event-driven-architecture/src/main/java/com/iluwatar/eda/App.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323
package com.iluwatar.eda;
2424

25-
import com.iluwatar.eda.event.Event;
2625
import com.iluwatar.eda.event.UserCreatedEvent;
2726
import com.iluwatar.eda.event.UserUpdatedEvent;
27+
import com.iluwatar.eda.framework.Event;
2828
import com.iluwatar.eda.framework.EventDispatcher;
2929
import com.iluwatar.eda.handler.UserCreatedEventHandler;
3030
import com.iluwatar.eda.handler.UserUpdatedEventHandler;
@@ -53,12 +53,12 @@ public class App {
5353
public static void main(String[] args) {
5454

5555
EventDispatcher dispatcher = new EventDispatcher();
56-
dispatcher.registerChannel(UserCreatedEvent.class, new UserCreatedEventHandler());
57-
dispatcher.registerChannel(UserUpdatedEvent.class, new UserUpdatedEventHandler());
56+
dispatcher.registerHandler(UserCreatedEvent.class, new UserCreatedEventHandler());
57+
dispatcher.registerHandler(UserUpdatedEvent.class, new UserUpdatedEventHandler());
5858

5959
User user = new User("iluwatar");
60-
dispatcher.onEvent(new UserCreatedEvent(user));
61-
dispatcher.onEvent(new UserUpdatedEvent(user));
60+
dispatcher.dispatch(new UserCreatedEvent(user));
61+
dispatcher.dispatch(new UserUpdatedEvent(user));
6262
}
6363

6464
}

event-driven-architecture/src/main/java/com/iluwatar/eda/event/Event.java renamed to event-driven-architecture/src/main/java/com/iluwatar/eda/event/AbstractEvent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@
2323
package com.iluwatar.eda.event;
2424

2525
import com.iluwatar.eda.framework.EventDispatcher;
26-
import com.iluwatar.eda.framework.Message;
26+
import com.iluwatar.eda.framework.Event;
2727

2828
/**
29-
* The {@link Event} class serves as a base class for defining custom events happening with your
29+
* The {@link AbstractEvent} class serves as a base class for defining custom events happening with your
3030
* system. In this example we have two types of events defined.
3131
* <ul>
3232
* <li>{@link UserCreatedEvent} - used when a user is created</li>
3333
* <li>{@link UserUpdatedEvent} - used when a user is updated</li>
3434
* </ul>
3535
* Events can be distinguished using the {@link #getType() getType} method.
3636
*/
37-
public class Event implements Message {
37+
public abstract class AbstractEvent implements Event {
3838

3939
/**
4040
* Returns the event type as a {@link Class} object
4141
* In this example, this method is used by the {@link EventDispatcher} to
4242
* dispatch events depending on their type.
4343
*
44-
* @return the Event type as a {@link Class}.
44+
* @return the AbstractEvent type as a {@link Class}.
4545
*/
46-
public Class<? extends Message> getType() {
46+
public Class<? extends Event> getType() {
4747
return getClass();
4848
}
4949
}

event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserCreatedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* This class can be extended to contain details about the user has been created. In this example,
3030
* the entire {@link User} object is passed on as data with the event.
3131
*/
32-
public class UserCreatedEvent extends Event {
32+
public class UserCreatedEvent extends AbstractEvent {
3333

3434
private User user;
3535

event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserUpdatedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* This class can be extended to contain details about the user has been updated. In this example,
3030
* the entire {@link User} object is passed on as data with the event.
3131
*/
32-
public class UserUpdatedEvent extends Event {
32+
public class UserUpdatedEvent extends AbstractEvent {
3333

3434
private User user;
3535

event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Message.java renamed to event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Event.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
package com.iluwatar.eda.framework;
2424

2525
/**
26-
* A {@link Message} is an object with a specific type that is associated
26+
* A {@link Event} is an object with a specific type that is associated
2727
* to a specific {@link Handler}.
2828
*/
29-
public interface Message {
29+
public interface Event {
3030

3131
/**
3232
* Returns the message type as a {@link Class} object. In this example the message type is
3333
* used to handle events by their type.
3434
* @return the message type as a {@link Class}.
3535
*/
36-
Class<? extends Message> getType();
36+
Class<? extends Event> getType();
3737
}

event-driven-architecture/src/main/java/com/iluwatar/eda/framework/EventDispatcher.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,16 @@
2222
*/
2323
package com.iluwatar.eda.framework;
2424

25-
import com.iluwatar.eda.event.Event;
26-
2725
import java.util.HashMap;
2826
import java.util.Map;
2927

3028
/**
3129
* Handles the routing of {@link Event} messages to associated handlers.
3230
* A {@link HashMap} is used to store the association between events and their respective handlers.
33-
*
3431
*/
3532
public class EventDispatcher {
3633

37-
private Map<Class<? extends Event>, Handler<?>> handlers;
34+
private Map<Class<? extends Event>, Handler<? extends Event>> handlers;
3835

3936
public EventDispatcher() {
4037
handlers = new HashMap<>();
@@ -46,8 +43,8 @@ public EventDispatcher() {
4643
* @param eventType The {@link Event} to be registered
4744
* @param handler The {@link Handler} that will be handling the {@link Event}
4845
*/
49-
public void registerChannel(Class<? extends Event> eventType,
50-
Handler<?> handler) {
46+
public <E extends Event> void registerHandler(Class<E> eventType,
47+
Handler<E> handler) {
5148
handlers.put(eventType, handler);
5249
}
5350

@@ -56,8 +53,12 @@ public void registerChannel(Class<? extends Event> eventType,
5653
*
5754
* @param event The {@link Event} to be dispatched
5855
*/
59-
public void onEvent(Event event) {
60-
handlers.get(event.getClass()).onEvent(event);
56+
@SuppressWarnings("unchecked")
57+
public <E extends Event> void dispatch(E event) {
58+
Handler<E> handler = (Handler<E>) handlers.get(event.getClass());
59+
if (handler != null) {
60+
handler.onEvent(event);
61+
}
6162
}
6263

6364
}

0 commit comments

Comments
 (0)