diff --git a/event-driven-architecture/index.md b/event-driven-architecture/index.md new file mode 100644 index 000000000..d5b501c3a --- /dev/null +++ b/event-driven-architecture/index.md @@ -0,0 +1,31 @@ +layout: pattern +title: Event Driven Architecture +folder: event-driven-architecture +permalink: /patterns/event-driven-architecture + + +**Intent:** Send and notify state changes of your objects to other applications using an Event-driven Architecture. + +![alt text](./etc/class_diagram.png "Event Driven Architecture") + +**Applicability:** Use an Event-driven architecture when + +* you want to create a loosely coupled system +* you want to build a more responsive system +* you want a system that is easier to extend + +**Real world examples:** + +* A Loan Application has been accepted/rejected (commercial business). +* A new Rostering Schedule is ready for distribution to all crew (Airline Management System). +* An Illegal Trade Pattern has been detected (Trading Fraud Detection System). +* A simulated car has hits another simulated car (Commercial Racing Game). +* A robot has reached its destination (Real Time Warehouse Management System). +* A HTML message has been received (Web Server). +* A key has been pressed (Text Editor). + +**Credits:** + +* [Event-driven architecture - Wikipedia](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained) +* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html) +* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications) \ No newline at end of file diff --git a/event-driven-architecture/pom.xml b/event-driven-architecture/pom.xml new file mode 100644 index 000000000..fdea3855f --- /dev/null +++ b/event-driven-architecture/pom.xml @@ -0,0 +1,22 @@ + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.8.0-SNAPSHOT + + + event-driven-architecture + + + + junit + junit + test + + + \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/App.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/App.java new file mode 100644 index 000000000..bcd2120d8 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/App.java @@ -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()); + } +} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Channel.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Channel.java new file mode 100644 index 000000000..a977010c6 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Channel.java @@ -0,0 +1,6 @@ +package com.iluwatar.eda.advanced; + + +public interface Channel { + public void dispatch(E message); +} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/DynamicRouter.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/DynamicRouter.java new file mode 100644 index 000000000..f72b79150 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/DynamicRouter.java @@ -0,0 +1,6 @@ +package com.iluwatar.eda.advanced; + +public interface DynamicRouter { + public void registerChannel(Class contentType, Channel channel); + public void dispatch(E content); +} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Event.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Event.java new file mode 100644 index 000000000..8ad48c704 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Event.java @@ -0,0 +1,7 @@ +package com.iluwatar.eda.advanced; + +public class Event implements Message { + public Class getType() { + return getClass(); + } +} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/EventDispatcher.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/EventDispatcher.java new file mode 100644 index 000000000..def8920c1 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/EventDispatcher.java @@ -0,0 +1,21 @@ +package com.iluwatar.eda.advanced; + +import java.util.HashMap; +import java.util.Map; + +public class EventDispatcher implements DynamicRouter { + private Map, Handler> handlers; + + public EventDispatcher() { + handlers = new HashMap, Handler>(); + } + + public void registerChannel(Class contentType, + Channel channel) { + handlers.put(contentType, (Handler)channel); + } + + public void dispatch(Event content) { + handlers.get(content.getClass()).dispatch(content); + } +} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Handler.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Handler.java new file mode 100644 index 000000000..68631b207 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Handler.java @@ -0,0 +1,7 @@ +package com.iluwatar.eda.advanced; + +public class Handler implements Channel { + public void dispatch(Event message) { + System.out.println(message.getClass()); + } +} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Message.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Message.java new file mode 100644 index 000000000..00d335191 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/Message.java @@ -0,0 +1,6 @@ +package com.iluwatar.eda.advanced; + + +public interface Message { + public Class getType(); +} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/App.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/App.java new file mode 100644 index 000000000..1399811bb --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/App.java @@ -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. + *

+ * 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 events = new LinkedList(); + 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); + } + } +} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/Event.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/Event.java new file mode 100644 index 000000000..1ba04c303 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/Event.java @@ -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; + } +} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/EventHandler.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/EventHandler.java new file mode 100644 index 000000000..d911c6c8e --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/EventHandler.java @@ -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()); + } +}