Browser docs

Data Bus


Allows send of messages/events between components of an application without them needing to know about each other. They only need to know about the type of the message/event being sent.


Real world example

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.

In plain words

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.

Programmatic Example

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).

1public interface Member extends Consumer<DataType> {
3  void accept(DataType event);

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.

 1 public class DataBus {
 3  private static final DataBus INSTANCE = new DataBus();
 5  private final Set<Member> listeners = new HashSet<>();
 7  public static DataBus getInstance() {
 8    return INSTANCE;
 9  }
11  /**
12   * Register a member with the data-bus to start receiving events.
13   *
14   * @param member The member to register
15   */
16  public void subscribe(final Member member) {
18    this.listeners.add(member);
19  }
21  /**
22   * Deregister a member to stop receiving events.
23   *
24   * @param member The member to deregister
25   */
26  public void unsubscribe(final Member member) {
27    this.listeners.remove(member);
28  }
30  /**
31   * Publish and event to all members.
32   *
33   * @param event The event
34   */
35  public void publish(final DataType event) {
36    event.setDataBus(this);
38    listeners.forEach(
39            listener -> listener.accept(event));
40  }

As you can see, the accept method is applied for each member under the publish method.

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).

 1public class MessageCollectorMember implements Member {
 3  private final String name;
 5  private final List<String> messages = new ArrayList<>();
 7  public MessageCollectorMember(String name) {
 8 = name;
 9  }
11  @Override
12  public void accept(final DataType data) {
13    if (data instanceof MessageData) {
14      handleEvent((MessageData) data);
15    }
16  }

However, the StatusMember(the event administrators or organisers) can accept such types of messages as

 1public class StatusMember implements Member {
 3  private final int id;
 5  private LocalDateTime started;
 7  private LocalDateTime stopped;
 8  @Override
 9  public void accept(final DataType data) {
10    if (data instanceof StartingData) {
11      handleEvent((StartingData) data);
12    } else if (data instanceof StoppingData) {
13      handleEvent((StoppingData) data);
14    }
15  }

Thus, the data bus outputs as follows:

 1class App {
 3 public static void main(String[] args) {
 4   final var bus = DataBus.getInstance();
 5   bus.subscribe(new StatusMember(1));
 6   bus.subscribe(new StatusMember(2));
 7   final var foo = new MessageCollectorMember("Foo");
 8   final var bar = new MessageCollectorMember("Bar");
 9   bus.subscribe(foo);
10   bus.publish(StartingData.of(;


//02:33:57.627 [main] INFO com.iluwatar.databus.members.StatusMember - Receiver 2 sees application started at 2022-10-26T02:33:57.613529100

//02:33:57.633 [main] INFO com.iluwatar.databus.members.StatusMember - Receiver 1 sees application started at 2022-10-26T02:33:57.613529100

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.

Class diagram

data bus pattern uml diagram


Use Data Bus pattern when

  • you want your components to decide themselves which messages/events they want to receive
  • you want to have many-to-many communication
  • you want your components to know nothing about each other

Data Bus is similar to

  • Mediator pattern with Data Bus Members deciding for themselves if they want to accept any given message
  • Observer pattern but supporting many-to-many communication
  • Publish/Subscribe pattern with the Data Bus decoupling the publisher and the subscriber