Search in sources :

Example 1 with EventDispatcher

use of com.iluwatar.eda.framework.EventDispatcher in project java-design-patterns by iluwatar.

the class App method main.

/**
 * 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) {
    EventDispatcher dispatcher = new EventDispatcher();
    dispatcher.registerHandler(UserCreatedEvent.class, new UserCreatedEventHandler());
    dispatcher.registerHandler(UserUpdatedEvent.class, new UserUpdatedEventHandler());
    User user = new User("iluwatar");
    dispatcher.dispatch(new UserCreatedEvent(user));
    dispatcher.dispatch(new UserUpdatedEvent(user));
}
Also used : UserCreatedEvent(com.iluwatar.eda.event.UserCreatedEvent) EventDispatcher(com.iluwatar.eda.framework.EventDispatcher) User(com.iluwatar.eda.model.User) UserCreatedEventHandler(com.iluwatar.eda.handler.UserCreatedEventHandler) UserUpdatedEvent(com.iluwatar.eda.event.UserUpdatedEvent) UserUpdatedEventHandler(com.iluwatar.eda.handler.UserUpdatedEventHandler)

Aggregations

UserCreatedEvent (com.iluwatar.eda.event.UserCreatedEvent)1 UserUpdatedEvent (com.iluwatar.eda.event.UserUpdatedEvent)1 EventDispatcher (com.iluwatar.eda.framework.EventDispatcher)1 UserCreatedEventHandler (com.iluwatar.eda.handler.UserCreatedEventHandler)1 UserUpdatedEventHandler (com.iluwatar.eda.handler.UserUpdatedEventHandler)1 User (com.iluwatar.eda.model.User)1