sensorsystem.java~8~

来自「《深入浅出设计模式》的完整源代码」· JAVA~8~ 代码 · 共 66 行

JAVA~8~
66
字号
package sensorsystem;

class SensorSystem {
  private java.util.Vector listeners = new java.util.Vector ();

  public void register (AlarmListener al) {
    listeners.addElement (al);
  }

  public void soundTheAlarm () {
    System.out.println("传感器检测有人入侵");
    for (java.util.Enumeration e = listeners.elements (); e.hasMoreElements (); )
      ( (AlarmListener) e.nextElement ()).alarm ();
  }
}

class Lighting
    implements AlarmListener {
  public void alarm () {
    System.out.println ("自动打开灯");
  }
}

class Gates
    implements AlarmListener {
  public void alarm () {
    System.out.println ("自动关上门");
  }
}

class CheckList {
  public void byTheNumbers () { // Template Method design pattern
    localize ();
    isolate ();
    identify ();
  }

  protected void localize () {
    System.out.println ("系统自动建立周界");
  }

  protected void isolate () {
    System.out.println ("隔离分析栅格");
  }

  protected void identify () {
    System.out.println ("确认入侵源");
  }
}

// class inheri. // type inheritance
class Surveillance
    extends CheckList
    implements AlarmListener {
  public void alarm () {
    System.out.println ("自动监视启动:");
    byTheNumbers ();
  }

  protected void isolate () {
    System.out.println ("摄像枪描准");
  }
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?