RuneHive-Game
Loading...
Searching...
No Matches
DataBus.java
Go to the documentation of this file.
1package com.runehive.game.event.bus;
2
3import com.runehive.game.event.Event;
4import com.runehive.game.event.listener.EventListener;
5
6import java.util.LinkedHashSet;
7import java.util.Set;
8
9/**
10 * <h2>A Databus implementation for sending events.</h2>
11 * <p>
12 * The difference between a DataBus and publish/subscribe model is that.
13 * A DataBus is a form of publish/subscribe, the only difference is that
14 * the bus decouples the publisher and subscriber.
15 *
16 * This pattern is different from the observer pattern in the sense that
17 * the observer pattern only allows for a one-to-many relationship wheres
18 * the DataBus allows for a many-to-many relationship.
19 *
20 * {@link EventListener}'s must subscribe to the bus
21 * in order to receive {@link Event}'s send by the bus.
22 * {@link EventListener}'s can then decide which {@link Event}'s
23 * they are interested in.
24 * </p>
25 *
26 * @see <a href="http://wiki.c2.com/?DataBusPattern">DataBusPattern</a>
27 *
28 * @author nshusa
29 */
30public final class DataBus {
31
32 /**
33 * The collection of listeners that are subscribed to this bus.
34 */
35 private static final Set<EventListener> listeners = new LinkedHashSet<>();
36
37 /**
38 * The singleton object.
39 */
40 private static final DataBus INSTANCE = new DataBus();
41
42 /**
43 * Prevent instantiation
44 */
45 private DataBus() {
46
47 }
48
49 /**
50 * Gets the singleton object.
51 */
52 public static DataBus getInstance() {
53 return INSTANCE;
54 }
55
56 /**
57 * Subscribes an {@link EventListener} to this bus.
58 *
59 * If a listener is subscribed to this bus,
60 * then the listener can choose which {@link Event}'s its interested in.
61 *
62 * @param listener
63 * The listener to subscribe to this bus.
64 */
67 }
68
69 /**
70 * Unsubscribe an {@link EventListener} from this bus.
71 *
72 * If a listener is no longer subscribed to this bus then
73 * the listener will no longer receive any events
74 * sent from the bus.
75 *
76 * @param listener
77 * The listener to unsubscribe from this bus.
78 */
80 listeners.remove(listener);
81 }
82
83 /**
84 * Sends an {@link Event} to all subscribed listeners.
85 *
86 * The listener's will choose which event.
87 * they are interested in.
88 *
89 * @param event
90 * The event that will be sent to all listeners.
91 */
92 public void publish(Event event) {
93 listeners.forEach(it -> it.accept(event));
94 }
95
96}
static DataBus getInstance()
Gets the singleton object.
Definition DataBus.java:52
DataBus()
Prevent instantiation.
Definition DataBus.java:45
void publish(Event event)
Sends an Event to all subscribed listeners.
Definition DataBus.java:92
static final Set< EventListener > listeners
The collection of listeners that are subscribed to this bus.
Definition DataBus.java:35
void subscribe(EventListener listener)
Subscribes an EventListener to this bus.
Definition DataBus.java:65
void unsubscribe(EventListener listener)
Unsubscribe an EventListener from this bus.
Definition DataBus.java:79
static final DataBus INSTANCE
The singleton object.
Definition DataBus.java:40
The base event listener that will listen for any type of event.
Definition Event.kt:9