#113 Event Driven Architecture

- initial commit includes a simple and advanced example of Event-driven architecture
This commit is contained in:
chris
2015-11-22 19:40:07 +01:00
parent 092d48d150
commit eb396217d0
12 changed files with 184 additions and 0 deletions

View File

@ -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());
}
}

View File

@ -0,0 +1,6 @@
package com.iluwatar.eda.advanced;
public interface Channel<E extends Message> {
public void dispatch(E message);
}

View File

@ -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);
}

View File

@ -0,0 +1,7 @@
package com.iluwatar.eda.advanced;
public class Event implements Message {
public Class<? extends Message> getType() {
return getClass();
}
}

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -0,0 +1,6 @@
package com.iluwatar.eda.advanced;
public interface Message {
public Class<? extends Message> getType();
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}