Using observer mode to design an event handler for school bell

Analysis: in this example, the school's "bell" is the event source and target, "teacher" and "student" are the event listener and specific observer, "bell" is the event class. When students and teachers come to the teaching area of the school, they will pay attention to the school bell, which is called event binding; When the time for class or class is up, the bell will sound, and a "bell" event will be generated; When students and teachers hear the bell, they will start or end class, which is called event handling. This example is very suitable to be implemented in observer mode. Figure 3 shows the event model of school ringtone.

Figure 3 event model of school bell

Now use "observer mode" to implement the event processing model.

First, define a RingEvent class, which records the type of ringtone (class ringtone / class ringtone).

Then define a school bell eventsource class, which is an event source and an observer target class. This class contains a listener container listener, which can bind listeners (students or teachers), and has methods to generate bell events and notify all listeners.

Then, define the bell event listener class, which is an abstract observer and contains the bell event processing method heardBell(RingEvent e).

Finally, define teacher class (TeachEventListener) and student class (StuEventListener). They are event listeners and specific observers. They will go to class or finish class when they hear the bell. Figure 4 shows the structure of the school ringtone event handler.

Figure 4 structure diagram of school ringtone event handler

import java.util.*;

public class BellEventTest {
    public static void main(String[] args) {
        BellEventSource bell = new BellEventSource();    //Bell (event source)
        bell.addPersonListener(new TeachEventListener()); //Registered listener (teacher)
        bell.addPersonListener(new StuEventListener());    //Registered listener (student)
        bell.ring(true);   //Ring the bell for class
        bell.ring(false);  //Ring the bell after class

//Ringtone event class: used to encapsulate the event source and some event related parameters
class RingEvent extends EventObject {
    private static final long serialVersionUID = 1L;
    private boolean sound;    //true indicates the bell for class and false indicates the bell for class

    public RingEvent(Object source, boolean sound) {
        this.sound = sound;

    public void setSound(boolean sound) {
        this.sound = sound;

    public boolean getSound() {
        return this.sound;

//Target class: event source, bell
class BellEventSource {
    private List<BellEventListener> listener; //Listener container

    public BellEventSource() {
        listener = new ArrayList<BellEventListener>();

    //Binding listeners to event sources
    public void addPersonListener(BellEventListener ren) {

    //Event trigger: ring the bell. When the value of the ring sound changes, an event will be triggered.
    public void ring(boolean sound) {
        String type = sound ? "Class bell" : "recess bell";
        System.out.println(type + "Ring!");
        RingEvent event = new RingEvent(this, sound);
        notifies(event);    //Notifies all listeners registered on the event source

    //When an event occurs, all listeners bound to the event source are notified to respond (calling the event handling method)
    protected void notifies(RingEvent e) {
        BellEventListener ren = null;
        Iterator<BellEventListener> iterator = listener.iterator();
        while (iterator.hasNext()) {
            ren =;

//Abstract Observer class: ringtone event listener
interface BellEventListener extends EventListener {
    //Event handling method, hear the bell
    public void heardBell(RingEvent e);

//Specific observers: teacher event listener
class TeachEventListener implements BellEventListener {
    public void heardBell(RingEvent e) {
        if (e.getSound()) {
            System.out.println("The teacher is in class...");
        } else {
            System.out.println("The teacher is over...");

//Specific observers: Student event listeners
class StuEventListener implements BellEventListener {
    public void heardBell(RingEvent e) {
        if (e.getSound()) {
            System.out.println("Class, class...");
        } else {
            System.out.println("Class is over, students...");

The running results of the program are as follows:

Class bell rings!
The teacher is in class
 Class, class
The bell rings after class!
The teacher is over
 Students, class is over

Tags: Java

Posted on Fri, 22 Oct 2021 04:43:55 -0400 by grantf