mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 06:58:54 +00:00
fb2c026822
* Moves eip-aggregator to Java 11 * Moves eip-message-channel to Java 11 * Moves eip-publish-subscribe to Java 11 * Moves eip-splitter to Java 11 * Moves eip-wire-tap to Java 11 * Moves event-aggregator to Java 11 * Moves event-asynchronous to Java 11 * Moves event-driven-architecture to Java 11 * Moves event-queue to Java 11 * Moves event-sourcing to Java 11 * Moves execute-around to Java 11 * Moves extension-objects to Java 11
65 lines
3.0 KiB
Java
65 lines
3.0 KiB
Java
/*
|
|
* The MIT License
|
|
* Copyright © 2014-2019 Ilkka Seppälä
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
package com.iluwatar.eda;
|
|
|
|
import com.iluwatar.eda.event.UserCreatedEvent;
|
|
import com.iluwatar.eda.event.UserUpdatedEvent;
|
|
import com.iluwatar.eda.framework.Event;
|
|
import com.iluwatar.eda.framework.EventDispatcher;
|
|
import com.iluwatar.eda.handler.UserCreatedEventHandler;
|
|
import com.iluwatar.eda.handler.UserUpdatedEventHandler;
|
|
import com.iluwatar.eda.model.User;
|
|
|
|
/**
|
|
* An event-driven architecture (EDA) is a framework that orchestrates behavior around the
|
|
* production, detection and consumption of events as well as the responses they evoke. An event is
|
|
* any identifiable occurrence that has significance for system hardware or software.
|
|
*
|
|
* <p>The example below uses an {@link EventDispatcher} to link/register {@link Event} objects to
|
|
* their respective handlers once an {@link Event} is dispatched, it's respective handler is invoked
|
|
* and the {@link Event} is handled accordingly.
|
|
*/
|
|
public class App {
|
|
|
|
/**
|
|
* Once the {@link EventDispatcher} is initialised, handlers related to specific events have to be
|
|
* made known to the dispatcher by registering them. In this case the {@link UserCreatedEvent} is
|
|
* bound to the UserCreatedEventHandler, whilst the {@link UserUpdatedEvent} is bound to the
|
|
* {@link UserUpdatedEventHandler}. The dispatcher can now be called to dispatch specific events.
|
|
* When a user is saved, the {@link UserCreatedEvent} can be dispatched. On the other hand, when a
|
|
* user is updated, {@link UserUpdatedEvent} can be dispatched.
|
|
*/
|
|
public static void main(String[] args) {
|
|
|
|
var dispatcher = new EventDispatcher();
|
|
dispatcher.registerHandler(UserCreatedEvent.class, new UserCreatedEventHandler());
|
|
dispatcher.registerHandler(UserUpdatedEvent.class, new UserUpdatedEventHandler());
|
|
|
|
var user = new User("iluwatar");
|
|
dispatcher.dispatch(new UserCreatedEvent(user));
|
|
dispatcher.dispatch(new UserUpdatedEvent(user));
|
|
}
|
|
|
|
}
|