Skip to content

Commit eb39621

Browse files
author
chris
committed
iluwatar#113 Event Driven Architecture
- initial commit includes a simple and advanced example of Event-driven architecture
1 parent 092d48d commit eb39621

12 files changed

Lines changed: 184 additions & 0 deletions

File tree

event-driven-architecture/index.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
layout: pattern
2+
title: Event Driven Architecture
3+
folder: event-driven-architecture
4+
permalink: /patterns/event-driven-architecture
5+
6+
7+
**Intent:** Send and notify state changes of your objects to other applications using an Event-driven Architecture.
8+
9+
![alt text](./etc/class_diagram.png "Event Driven Architecture")
10+
11+
**Applicability:** Use an Event-driven architecture when
12+
13+
* you want to create a loosely coupled system
14+
* you want to build a more responsive system
15+
* you want a system that is easier to extend
16+
17+
**Real world examples:**
18+
19+
* A Loan Application has been accepted/rejected (commercial business).
20+
* A new Rostering Schedule is ready for distribution to all crew (Airline Management System).
21+
* An Illegal Trade Pattern has been detected (Trading Fraud Detection System).
22+
* A simulated car has hits another simulated car (Commercial Racing Game).
23+
* A robot has reached its destination (Real Time Warehouse Management System).
24+
* A HTML message has been received (Web Server).
25+
* A key has been pressed (Text Editor).
26+
27+
**Credits:**
28+
29+
* [Event-driven architecture - Wikipedia](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
30+
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html)
31+
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)

event-driven-architecture/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
<parent>
8+
<groupId>com.iluwatar</groupId>
9+
<artifactId>java-design-patterns</artifactId>
10+
<version>1.8.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>event-driven-architecture</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<scope>test</scope>
20+
</dependency>
21+
</dependencies>
22+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar.eda.advanced;
2+
3+
public class App {
4+
5+
public static void main(String[] args) {
6+
EventDispatcher dispatcher = new EventDispatcher();
7+
dispatcher.registerChannel(Event.class, new Handler());
8+
dispatcher.dispatch(new Event());
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.iluwatar.eda.advanced;
2+
3+
4+
public interface Channel<E extends Message> {
5+
public void dispatch(E message);
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.iluwatar.eda.advanced;
2+
3+
public interface DynamicRouter<E extends Message> {
4+
public void registerChannel(Class<? extends E> contentType, Channel<? extends E> channel);
5+
public void dispatch(E content);
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.eda.advanced;
2+
3+
public class Event implements Message {
4+
public Class<? extends Message> getType() {
5+
return getClass();
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar.eda.advanced;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class EventDispatcher implements DynamicRouter<Event> {
7+
private Map<Class<? extends Event>, Handler> handlers;
8+
9+
public EventDispatcher() {
10+
handlers = new HashMap<Class<? extends Event>, Handler>();
11+
}
12+
13+
public void registerChannel(Class<? extends Event> contentType,
14+
Channel<? extends Event> channel) {
15+
handlers.put(contentType, (Handler)channel);
16+
}
17+
18+
public void dispatch(Event content) {
19+
handlers.get(content.getClass()).dispatch(content);
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.eda.advanced;
2+
3+
public class Handler implements Channel<Event> {
4+
public void dispatch(Event message) {
5+
System.out.println(message.getClass());
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.iluwatar.eda.advanced;
2+
3+
4+
public interface Message {
5+
public Class<? extends Message> getType();
6+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.eda.simple;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* Event-driven architecture (EDA) is a software architecture pattern promoting
8+
* the production, detection, consumption of, and reaction to events.
9+
* <p/>
10+
* This main class publishes events to queue. Each event on the queue is consumed
11+
* and handled depending on the type defined in the {@link Event}
12+
*/
13+
public class App {
14+
15+
public static void main(String args[]) {
16+
17+
//create a list of events having different types
18+
//add these events to a simple queue
19+
Queue<Event> events = new LinkedList<Event>();
20+
events.add(new Event('A', "Hello"));
21+
events.add(new Event('B', "event-driven"));
22+
events.add(new Event('A', "world!"));
23+
24+
Event e;
25+
26+
//the event loop will go through the list of events
27+
//and handle each one depending on it's type
28+
while (!events.isEmpty()) {
29+
e = events.remove();
30+
31+
//handle events depending on their type
32+
if (e.type == 'A')
33+
EventHandler.handleEventA(e);
34+
if (e.type == 'B')
35+
EventHandler.handleEventB(e);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)