Implementing The Observer Pattern In Java

Article by:
Date Published:
Last Modified:

Overview

Roll-Your-Own

The following examples shows you how to manually implement the Observer pattern in Java.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// This is an interface, describing what method a Listener class
// should contain
public interface HelloListener {
    void notify(String msg);
}

// Let's make a class which listens to "Hello" events
public class MyClass implements HelloListener {
    void notify(String msg) {
        System.out.println(msg);
    }
}

// Let's make a class which generates "Hello" events
public class HelloEventGenerator {

    List<HelloListener> helloListeners = new ArrayList<HelloListener>();

    public void addHelloListener(HelloListener helloListener) {
        helloListeners.add(helloListener);
    }

    public void raiseEvent() {
        for(HelloListener helloListener : helloListeners) {
            helloListener.notify("Hello, world!);
        }
    }

}

Since the interface is public, it has to be described in it’s own file.

java.util.Observer/Observable

The Java framework provides the java.lang.Observer and java.lang.Observable classes to standardise the way the observer pattern is implemented.

The problem is you can attach any Observer object to any Observable object, creating potential incorrect-type issues, especially when a project starts to grow in size.


Authors

Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .

Tags

    comments powered by Disqus