#113 Event Driven Architecture
- initial commit includes a simple and advanced example of Event-driven architecture
This commit is contained in:
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.eda.advanced;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
EventDispatcher dispatcher = new EventDispatcher();
|
||||
dispatcher.registerChannel(Event.class, new Handler());
|
||||
dispatcher.dispatch(new Event());
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.iluwatar.eda.advanced;
|
||||
|
||||
|
||||
public interface Channel<E extends Message> {
|
||||
public void dispatch(E message);
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.iluwatar.eda.advanced;
|
||||
|
||||
public interface DynamicRouter<E extends Message> {
|
||||
public void registerChannel(Class<? extends E> contentType, Channel<? extends E> channel);
|
||||
public void dispatch(E content);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.iluwatar.eda.advanced;
|
||||
|
||||
public class Event implements Message {
|
||||
public Class<? extends Message> getType() {
|
||||
return getClass();
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.iluwatar.eda.advanced;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class EventDispatcher implements DynamicRouter<Event> {
|
||||
private Map<Class<? extends Event>, Handler> handlers;
|
||||
|
||||
public EventDispatcher() {
|
||||
handlers = new HashMap<Class<? extends Event>, Handler>();
|
||||
}
|
||||
|
||||
public void registerChannel(Class<? extends Event> contentType,
|
||||
Channel<? extends Event> channel) {
|
||||
handlers.put(contentType, (Handler)channel);
|
||||
}
|
||||
|
||||
public void dispatch(Event content) {
|
||||
handlers.get(content.getClass()).dispatch(content);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.iluwatar.eda.advanced;
|
||||
|
||||
public class Handler implements Channel<Event> {
|
||||
public void dispatch(Event message) {
|
||||
System.out.println(message.getClass());
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.iluwatar.eda.advanced;
|
||||
|
||||
|
||||
public interface Message {
|
||||
public Class<? extends Message> getType();
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.iluwatar.eda.simple;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* Event-driven architecture (EDA) is a software architecture pattern promoting
|
||||
* the production, detection, consumption of, and reaction to events.
|
||||
* <p/>
|
||||
* This main class publishes events to queue. Each event on the queue is consumed
|
||||
* and handled depending on the type defined in the {@link Event}
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
//create a list of events having different types
|
||||
//add these events to a simple queue
|
||||
Queue<Event> events = new LinkedList<Event>();
|
||||
events.add(new Event('A', "Hello"));
|
||||
events.add(new Event('B', "event-driven"));
|
||||
events.add(new Event('A', "world!"));
|
||||
|
||||
Event e;
|
||||
|
||||
//the event loop will go through the list of events
|
||||
//and handle each one depending on it's type
|
||||
while (!events.isEmpty()) {
|
||||
e = events.remove();
|
||||
|
||||
//handle events depending on their type
|
||||
if (e.type == 'A')
|
||||
EventHandler.handleEventA(e);
|
||||
if (e.type == 'B')
|
||||
EventHandler.handleEventB(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.iluwatar.eda.simple;
|
||||
|
||||
/**
|
||||
* The Event class defines the type of event and data related to the event.
|
||||
*/
|
||||
public class Event {
|
||||
|
||||
public char type;
|
||||
public String data;
|
||||
|
||||
public Event(char type, String data){
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.iluwatar.eda.simple;
|
||||
|
||||
/**
|
||||
* The {@link EventHandler} class handles performs actions on {@link Event} objects
|
||||
*/
|
||||
public class EventHandler {
|
||||
|
||||
public static void handleEventA(Event e){
|
||||
System.out.println(e.data);
|
||||
}
|
||||
|
||||
public static void handleEventB(Event e){
|
||||
System.out.println(e.data.toUpperCase());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user