"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to implement custom events using observer pattern in Java?

How to implement custom events using observer pattern in Java?

Posted on 2025-04-30
Browse:904

How Can I Implement Custom Events in Java Using the Observer Pattern?

Creating Custom Events in Java

Custom events are indispensable in many programming scenarios, enabling components to communicate with each other based on specific triggers. This article aims to address the following:

Problem Statement

How can we implement custom events in Java to facilitate interactions between objects based on specific events?

Solution

The Observer design pattern provides an elegant solution for managing custom events in Java. Here's an overview of the Observer pattern:

  • Observable: Defines an interface for classes that manage subscribers to events.
  • Observer: Defines an interface for classes that receive notifications for events from observables.

Sample Implementation

The following code snippet demonstrates how to create custom events using the Observer pattern:

import java.util.*;

// Observer interface - defines what an Observer should respond to
interface HelloListener {
    void someoneSaidHello();
}

// Observable class - defines how an Observable manages Observers
class Initiater {
    private List listeners = new ArrayList();

    // Method to add observers
    public void addListener(HelloListener toAdd) {
        listeners.add(toAdd);
    }

    // Method to notify observers
    public void sayHello() {
        System.out.println("Hello!!");
        for (HelloListener hl : listeners) {
            hl.someoneSaidHello();
        }
    }
}

// Observer class - defines how an Observer responds to events
class Responder implements HelloListener {
    @Override
    public void someoneSaidHello() {
        System.out.println("Hello there...");
    }
}

// Main class
class Test {
    public static void main(String[] args) {
        Initiater initiater = new Initiater();
        Responder responder = new Responder();

        initiater.addListener(responder);
        initiater.sayHello();
    }
}

Execution

When you run the above code, it creates objects for the Initiater (observable) and Responder (observer). The Initiater adds the Responder as an observer, and when it fires the "hello" event (sayHello()), the Responder is notified and responds with its assigned action. This demonstrates the implementation of custom events in Java using the Observer pattern.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3