mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 22:59:11 +00:00
#113 Event Driven Architecture
Adds various changes including : - Fixes to Javadoc - Test refactoring and improvements - Refactored EventDispatcher to be immutable - Removed DynamicRouter interface since it not needed - Renamed Channel to a more appropriate name - Handler
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
package com.iluwatar.eda.framework;
|
||||
|
||||
import com.iluwatar.eda.event.Event;
|
||||
|
||||
/**
|
||||
* Channels are delivery points for messages. Every {@link Channel} is responsible for a single type
|
||||
* of message
|
||||
*/
|
||||
public interface Channel<E extends Message> {
|
||||
void dispatch(Event message);
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.iluwatar.eda.framework;
|
||||
|
||||
/**
|
||||
* A {@link DynamicRouter} is responsible for selecting the proper path of a {@link Message}
|
||||
* Messages can be associated to Channels through the registerChannel method and dispatched by
|
||||
* calling the dispatch method.
|
||||
*/
|
||||
public interface DynamicRouter<E extends Message> {
|
||||
|
||||
void registerChannel(Class<? extends E> contentType, Channel<?> channel);
|
||||
|
||||
void dispatch(E content);
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.iluwatar.eda.framework;
|
||||
|
||||
import com.iluwatar.eda.event.Event;
|
||||
import com.iluwatar.eda.framework.Handler;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Handles the routing of {@link Event} messages to associated handlers.
|
||||
* A {@link HashMap} is used to store the association between events and their respective handlers.
|
||||
*
|
||||
*/
|
||||
public class EventDispatcher {
|
||||
|
||||
private Map<Class<? extends Event>, Handler<?>> handlers;
|
||||
|
||||
public EventDispatcher() {
|
||||
handlers = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Links an {@link Event} to a specific {@link Handler}.
|
||||
*
|
||||
* @param eventType The {@link Event} to be registered
|
||||
* @param handler The {@link Handler} that will be handling the {@link Event}
|
||||
*/
|
||||
public void registerChannel(Class<? extends Event> eventType,
|
||||
Handler<?> handler) {
|
||||
handlers.put(eventType, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches an {@link Event} depending on it's type.
|
||||
*
|
||||
* @param event The {@link Event} to be dispatched
|
||||
*/
|
||||
public void onEvent(Event event) {
|
||||
handlers.get(event.getClass()).onEvent(event);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.iluwatar.eda.framework;
|
||||
|
||||
import com.iluwatar.eda.event.Event;
|
||||
|
||||
/**
|
||||
* This interface can be implemented to handle different types of messages.
|
||||
* Every handler is responsible for a single of type message
|
||||
*/
|
||||
public interface Handler<E extends Message> {
|
||||
|
||||
/**
|
||||
* The onEvent method should implement and handle behavior related to the event.
|
||||
* This can be as simple as calling another service to handle the event on publishing the event on
|
||||
* a queue to be consumed by other sub systems.
|
||||
* @param event the {@link Event} object to be handled.
|
||||
*/
|
||||
void onEvent(Event event);
|
||||
}
|
||||
@@ -2,8 +2,14 @@ package com.iluwatar.eda.framework;
|
||||
|
||||
/**
|
||||
* A {@link Message} is an object with a specific type that is associated
|
||||
* to a specific {@link Channel}.
|
||||
* to a specific {@link Handler}.
|
||||
*/
|
||||
public interface Message {
|
||||
|
||||
/**
|
||||
* Returns the message type as a {@link Class} object. In this example the message type is
|
||||
* used to handle events by their type.
|
||||
* @return the message type as a {@link Class}.
|
||||
*/
|
||||
Class<? extends Message> getType();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user