package com.test.lisener;
import java.util.ArrayList;
import java.util.List;
//An interface to be implemented by everyone interested in "Hello" events
interface HelloListener {
void someoneSaidHello();
}
//Someone who says "Hello"
class Initiater {
private List<HelloListener> listeners = new ArrayList<HelloListener>();
public void addListener(HelloListener toAdd) {
listeners.add(toAdd);
}
public void sayHello() {
System.out.println("Hello!!");
// Notify everybody that may be interested.
for (HelloListener hl : listeners)
hl.someoneSaidHello();
}
}
//Someone interested in "Hello" events
class Responder implements HelloListener {
@Override
public void someoneSaidHello() {
System.out.println("Hello there...");
}
}
class Responder2 implements HelloListener{
@Override
public void someoneSaidHello() {
System.out.println("hellll............");
}
}
class TestLisener {
public static void main(String[] args) {
Initiater initiater = new Initiater();
Responder responder = new Responder();
Responder2 responder2 = new Responder2();
initiater.addListener(responder);
initiater.addListener(responder2);
initiater.sayHello(); // Prints "Hello!!!" and "Hello there..."
}
}
输出
Hello!!
Hello there...
hellll............