在网上看到的一个例子,感觉很合适分享给大家!
package observer;
import java.util.*;
/**
* @Author ***
* @Date 2021/6/10 15:42
* @Email ***
* @Description: 观察者模式
*/
public class Observer {
public static void main(String[] args) {
BellEventSource bell = new BellEventSource(); //铃(事件源)
bell.addPersonListener(new TeachEventListener()); //注册监听器(老师)
bell.addPersonListener(new StuEventListener()); //注册监听器(学生)
bell.ring(true); //打上课铃声
System.out.println("------------");
bell.ring(false); //打下课铃声
}
}
//铃声事件类:用于封装事件源及一些与事件相关的参数
class RingEvent extends EventObject {
private static final long serialVersionUID = 1L;
private boolean sound; //true表示上课铃声,false表示下课铃声
public RingEvent(Object source, boolean sound) {
super(source);
this.sound = sound;
}
public void setSound(boolean sound) {
this.sound = sound;
}
public boolean getSound() {
return this.sound;
}
}
//目标类:事件源,铃(被观察者)
class BellEventSource {
private List<BellEventListener> listener; //监听器容器(装的是观察者)
public BellEventSource() {
listener = new ArrayList<BellEventListener>();
}
//给事件源绑定监听器
public void addPersonListener(BellEventListener ren) {
listener.add(ren);
}
//事件触发器:敲钟,当铃声sound的值发生变化时,触发事件。
public void ring(boolean sound) {
String type = sound ? "上课铃" : "下课铃";
System.out.println(type + "响!");
RingEvent event = new RingEvent(this, sound);
notifies(event); //通知注册在该事件源上的所有监听器
}
//当事件发生时,通知绑定在该事件源上的所有监听器做出反应(调用事件处理方法)
protected void notifies(RingEvent e) {
BellEventListener ren = null;
Iterator<BellEventListener> iterator = listener.iterator();
while (iterator.hasNext()) {
ren = iterator.next();
ren.heardBell(e);
}
}
}
//抽象观察者类:铃声事件监听器
interface BellEventListener extends EventListener {
//事件处理方法,听到铃声
public void heardBell(RingEvent e);
}
//具体观察者类:老师事件监听器
class TeachEventListener implements BellEventListener {
public void heardBell(RingEvent e) {
if (e.getSound()) {
System.out.println("老师上课了...");
} else {
System.out.println("老师下课了...");
}
}
}
//具体观察者类:学生事件监听器
class StuEventListener implements BellEventListener {
public void heardBell(RingEvent e) {
if (e.getSound()) {
System.out.println("同学们,上课了...");
} else {
System.out.println("同学们,下课了...");
}
}
}
原文地址:http://c.biancheng.net/view/1390.html