zoukankan      html  css  js  c++  java
  • java_Observer Design Pattern

    摘自:
        http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html
    
    Creating 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<LightListener> listeners = new ArrayList<LightListener>();
     
       /** 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 off
    LightWatcher-1: created
    LightWatcher-2: created
    LightWatcher-3: created
    Light: added a listener
    Light: added a listener
    Light: turn on
    LightWatcher-1: I am notified that light is on
    LightWatcher-2: I am notified that light is on
    Light: added a listener
    Light: turn off
    LightWatcher-1: I am notified that light is off
    LightWatcher-2: I am notified that light is off
    LightWatcher-3: I am notified that light is off
    Light: removed a listener
    Light: removed a listener
    Light: turn on
    LightWatcher-2: I am notified that light is on
  • 相关阅读:
    Maven3路程(五)用Maven创建Hibernate项目
    Eclipse中通过Hibernate Tools插件实现从数据库逆向生成Hibernate带注解的实体类
    Maven3路程(三)用Maven创建第一个web项目(1)
    jquery下载所有版本(实时更新)
    Oracle读取Blob数据-通过hibernate
    使用以下代码可以插入BLOB类型的图片或pdf文档到Oracle数据库中:
    Intellij IDEA系列 --GIT的初步使用
    java.lang.OutOfMemoryError GC overhead limit exceeded原因分析及解决方案
    idea如何安装使用jetty runner
    IntelliJ IDEA下SVN的配置及使用说明
  • 原文地址:https://www.cnblogs.com/helloworldtoyou/p/5149940.html
Copyright © 2011-2022 走看看