#113 Event Driven Architecture
- initial commit includes a simple and advanced example of Event-driven architecture
This commit is contained in:
		
							
								
								
									
										31
									
								
								event-driven-architecture/index.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								event-driven-architecture/index.md
									
									
									
									
									
										Normal file
									
								
							| @@ -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. | ||||
|  | ||||
|  | ||||
|  | ||||
| **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) | ||||
							
								
								
									
										22
									
								
								event-driven-architecture/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								event-driven-architecture/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|  | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
|     <parent> | ||||
|         <groupId>com.iluwatar</groupId> | ||||
|         <artifactId>java-design-patterns</artifactId> | ||||
|         <version>1.8.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
|  | ||||
|     <artifactId>event-driven-architecture</artifactId> | ||||
|  | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>junit</groupId> | ||||
|             <artifactId>junit</artifactId> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| </project> | ||||
| @@ -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