博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java_Observer Design Pattern
阅读量:5132 次
发布时间:2019-06-13

本文共 4683 字,大约阅读时间需要 15 分钟。

摘自:    http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.htmlCreating Your Own Event, Source and Listener:Suppose that we have a source called Light, with two states - on and off. The source is capable of notifying its registered listener(s), whenever its state changes.    First, we define the LightEvent class (extends from java.util.EventObject).    Next, we define a LightListener interface to bind the source and its listeners. This interface specifies the signature of the handlers, lightOn(LightEvent) and lightOff(LightEvent).    In the source Light, we use an ArrayList to maintain its listeners, and create two methods: addLightListner(LightListener) and removeLightListener(LightListener). An method called notifyListener() is written to invoke the appropriate handlers of each of its registered listeners, whenever the state of the Light changes.    A listener class called LightWatcher is written, which implements the LightListener interface and provides implementation for the handlers.Event: LightEvent.java/** LightEvent */import java.util.EventObject; public class LightEvent extends EventObject {   public LightEvent (Object src) {      super(src);   }}Listener Interface: LightListener.java/** The LightListener interface */import java.util.EventListener; public interface LightListener extends EventListener {   public void lightOn(LightEvent evt);  // called-back upon light on   public void lightOff(LightEvent evt); // called-back upon light off}Source: Light.java/** The Light Source */import java.util.*; public class Light {   // Status - on (true) or off (false)   private boolean on;   // Listener list   private List
listeners = new ArrayList
(); /** Constructor */ public Light() { on = false; System.out.println("Light: constructed and off"); } /** Add the given LightListener */ public void addLightListener(LightListener listener) { listeners.add(listener); System.out.println("Light: added a listener"); } /** Remove the given LightListener */ public void removeLightListener(LightListener listener) { listeners.remove(listener); System.out.println("Light: removed a listener"); } /** Turn on this light */ public void turnOn() { if (!on) { on = !on; System.out.println("Light: turn on"); notifyListener(); } } /** Turn off this light */ public void turnOff() { if (on) { on = !on; System.out.println("Light: turn off"); notifyListener(); } } /** Construct an LightEvent and notify all its registered listeners */ private void notifyListener() { LightEvent evt = new LightEvent(this); for (LightListener listener : listeners) { if (on) { listener.lightOn(evt); } else { listener.lightOff(evt); } } }}Listener: LightWatcher.java/** An implementation of LightListener class */public class LightWatcher implements LightListener { private int id; // ID of this listener /** Constructor */ public LightWatcher(int id) { this.id = id; System.out.println("LightWatcher-" + id + ": created"); } /** Implementation of event handlers */ @Override public void lightOn(LightEvent evt) { System.out.println("LightWatcher-" + id + ": I am notified that light is on"); } @Override public void lightOff(LightEvent evt) { System.out.println("LightWatcher-" + id + ": I am notified that light is off"); }}A Test Driver: TestLight.java/** A Test Driver */public class TestLight { public static void main(String[] args) { Light light = new Light(); LightWatcher lw1 = new LightWatcher(1); LightWatcher lw2 = new LightWatcher(2); LightWatcher lw3 = new LightWatcher(3); light.addLightListener(lw1); light.addLightListener(lw2); light.turnOn(); light.addLightListener(lw3); light.turnOff(); light.removeLightListener(lw1); light.removeLightListener(lw3); light.turnOn(); }}Below are the expected output:Light: constructed and offLightWatcher-1: createdLightWatcher-2: createdLightWatcher-3: createdLight: added a listenerLight: added a listenerLight: turn onLightWatcher-1: I am notified that light is onLightWatcher-2: I am notified that light is onLight: added a listenerLight: turn offLightWatcher-1: I am notified that light is offLightWatcher-2: I am notified that light is offLightWatcher-3: I am notified that light is offLight: removed a listenerLight: removed a listenerLight: turn onLightWatcher-2: I am notified that light is on

 

转载于:https://www.cnblogs.com/helloworldtoyou/p/5149940.html

你可能感兴趣的文章
HDU 2199 Can you solve this equation?【二分查找】
查看>>
【C#】【MySQL】C# 查询数据库语句@Row:=@Row+1以及执行存储过程失败解决方案
查看>>
分享到朋友圈实现
查看>>
计算机专业学习课程推荐
查看>>
Excel 将换行符替换为空
查看>>
tp5 前台 点击显示一个弹窗
查看>>
权威指南之脚本化jquery
查看>>
tomcat监控脚本
查看>>
一道计算时间的机试题
查看>>
SpringCloud - 2. 服务注册 和 发现
查看>>
selenium 学习之路开始了,一遍搬一遍理解学习,加油!!!
查看>>
SQL视频学习
查看>>
基于Jquery的图片自动分组且自适应页面的缩略图展示特效
查看>>
java操作properties文件
查看>>
20130401学习笔记
查看>>
Noip2018游记
查看>>
【转载】深入理解Java内存模型——final
查看>>
HDU 3694 Fermat Point in Quadrangle (计算几何- 四边形的费马点)
查看>>
关于foreach
查看>>
nodejs的req取参req.body,req.params,req.query
查看>>