Event Bus and Observer Pattern in Java: A Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Event Bus and Observer Pattern in Java



Modern applications are increasingly built on event-driven architecture. Two key tools for implementing such a model in Java are the Observer pattern and its evolution — the Event Bus. In this article, we will examine how they differ, how to implement each one, and which libraries can help simplify development.



What is the Observer Pattern?



The Observer pattern is a behavioral design pattern where one object (the subject) notifies multiple dependent objects (observers) about changes in its state. In Java, this pattern is often used in graphical interfaces, logging systems, and event processing.



Key components:

  • Subject — the source of events, manages the list of observers
  • Observer — an interface implemented by all subscribers
  • ConcreteSubject — a concrete implementation of the subject
  • ConcreteObserver — a concrete observer


Example implementation in plain Java:



// Observer interfaceinterface Observer {    void update(String event);}

// Subject interfaceinterface Subject { void attach(Observer observer); void detach(Observer observer); void notifyObservers(String event);}

// Concrete subjectclass EventManager implements Subject { private List<Observer> observers = new ArrayList<>(); @Override public void attach(Observer observer) { observers.add(observer); } @Override public void detach(Observer observer) { observers.remove(observer); } @Override public void notifyObservers(String event) { for (Observer observer : observers) { observer.update(event); } } public void fireEvent(String event) { System.out.println("Event: " + event); notifyObservers(event); }}

// Concrete observerclass LogObserver implements Observer { @Override public void update(String event) { System.out.println("Log: received event - " + event); }}

// Usagepublic class ObserverDemo { public static void main(String[] args) { EventManager manager = new EventManager(); manager.attach(new LogObserver()); manager.fireEvent("User logged in"); }}


What is an Event Bus?



An Event Bus is a centralized messaging mechanism for exchanging messages between application components. Unlike the classic Observer pattern, where the subject directly manages subscribers, the Event Bus decouples event senders and receivers through a common bus.



Advantages of Event Bus:

  • Loose coupling of components
  • Simplified subscription and publication
  • Support for event filtering by type
  • Asynchronous processing (optional)


Guava EventBus Library



Google Guava provides a powerful implementation of Event Bus. It is widely used in industrial projects and is easy to integrate.



Maven dependency:

<dependency>    <groupId>com.google.guava</groupId>    <artifactId>guava</artifactId>    <version>33.0.0-jre</version></dependency>


Usage example:



import com.google.common.eventbus.EventBus;import com.google.common.eventbus.Subscribe;

// Event classclass UserLoginEvent { private final String username; public UserLoginEvent(String username) { this.username = username; } public String getUsername() {

Recommendations