You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: data-bus/README.md
+134Lines changed: 134 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,140 @@ Allows send of messages/events between components of an application
12
12
without them needing to know about each other. They only need to know
13
13
about the type of the message/event being sent.
14
14
15
+
## Explanation
16
+
17
+
Real world example
18
+
19
+
> Say you have an app that enables online bookings and participation of events. You want the app to send notifications such as event advertisements to everyone who is an ordinary member of the community or organisation holding the events. However, you do not want to send such notifications like advertisements to the event administrators or organisers but you desire to send them and them only the time whenever a new advertisement is sent to all members of the community. The Data Bus enables you to selectively notify people of a community by type, whether it be ordinary community members or event administrators, by making their classes or components only accept messages of a certain type. Ultimately, there is no need for the components or classes of ordinary community members nor administrators to know anything about you in terms of the classes or components you are using to notify the entire community except for the need to know the type of the messages you are sending.
20
+
21
+
In plain words
22
+
23
+
> Data Bus is a design pattern that is able to connect components of an application for communication simply and solely by the type of message or event that may be transferred.
24
+
25
+
Programmatic Example
26
+
27
+
Translating the online events app example above, we firstly have our Member interface and its implementations composed of MessageCollectorMember (the ordinary community members) and StatusMember (the event administrators or organisers).
28
+
29
+
```java
30
+
publicinterfaceMemberextendsConsumer<DataType> {
31
+
32
+
voidaccept(DataTypeevent);
33
+
}
34
+
```
35
+
36
+
Then we have a databus to subscribe someone to be a member or unsubcribe, and also to publish an event so to notify every member in the community.
* Register a member with the data-bus to start receiving events.
51
+
*
52
+
* @param member The member to register
53
+
*/
54
+
publicvoidsubscribe(finalMembermember) {
55
+
56
+
this.listeners.add(member);
57
+
}
58
+
59
+
/**
60
+
* Deregister a member to stop receiving events.
61
+
*
62
+
* @param member The member to deregister
63
+
*/
64
+
publicvoidunsubscribe(finalMembermember) {
65
+
this.listeners.remove(member);
66
+
}
67
+
68
+
/**
69
+
* Publish and event to all members.
70
+
*
71
+
* @param event The event
72
+
*/
73
+
publicvoidpublish(finalDataTypeevent) {
74
+
event.setDataBus(this);
75
+
76
+
listeners.forEach(
77
+
listener -> listener.accept(event));
78
+
}
79
+
}
80
+
```
81
+
82
+
As you can see, the accept method is applied for each member under the publish method.
83
+
84
+
Hence, as shown below, the accept method can be used to check the type of message to be published and successfully send/handle that message if the accept method has an instance for that message. Otherwise, the accept method cannot as is for the case of the MessageCollectorMember (the ordinary community members) when the type of message being sent is StartingData or StoppingData (information on the time whenever a new advertisement is sent to all members).
//02:33:57.627 [main] INFO com.iluwatar.databus.members.StatusMember - Receiver 2 sees application started at 2022-10-26T02:33:57.613529100
144
+
145
+
//02:33:57.633 [main] INFO com.iluwatar.databus.members.StatusMember - Receiver 1 sees application started at 2022-10-26T02:33:57.613529100
146
+
147
+
Evidently, due to MessageCollectorMembers only accepting messages of type MessageData and none of either StartingData nor StoppingData, the MessageCollectorMembers are prevented from seeing what the StatusMembers (the event administrators or organisers) are shown: information on the time whenever a new advertisement is sent to all members.
148
+
15
149
## Class diagram
16
150

0 commit comments