package com.yqs.dp.observer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Test {
 public static void main(String[] args) {
  Child c = new Child();
  
  //用属性文件管理observers
  
  String[] observers = PropertyMgr.getProperty("observers").split(",");
//System.out.println(observers); 
  for(String s : observers) {
   try {
    System.out.println(s);
    c.addWakenUpListener((WakenUpListener)Class.forName(s).newInstance());
   } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  Dad d = new Dad();
  
  
  new Thread(c).start();
 }
}
class PropertyMgr {
 private static Properties props = new Properties();
 //load到内存。。只执行一遍---单例模式
 static {
  try {
   props.load(Test.class.getClassLoader().getResourceAsStream("com/yqs/dp/observer/observer.properties"));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public static String getProperty(String key) {  
  return props.getProperty(key);
 }
}
class WakenUpEvent {
 private long time;
 private String loc;
 private Child source;
 public WakenUpEvent(long time, String loc, Child source) {
  super();
  this.time = time;
  this.loc = loc;
  this.source = source;
 }
 public long getTime() {
  return time;
 }
 public void setTime(long time) {
  this.time = time;
 }
 public String getLoc() {
  return loc;
 }
 public void setLoc(String loc) {
  this.loc = loc;
 }
 public Child getSource() {
  return source;
 }
 public void setSource(Child source) {
  this.source = source;
 }
}
class Dog implements WakenUpListener {
 @Override
 public void ActionToWakenUp(WakenUpEvent arg0) {
  System.out.println("wang wang...");
  
 }
 
}
class Child implements Runnable{
 private List<WakenUpListener> wakenUpListeners = new ArrayList<WakenUpListener>();
 
 public void addWakenUpListener(WakenUpListener l) {
  wakenUpListeners.add(l);
 }
 
 public void run() {
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  this.wakenUp();
 }
 
 void wakenUp() {
  for(int i=0; i<wakenUpListeners.size(); i++) {
   WakenUpListener l = wakenUpListeners.get(i);
   l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
  }
 }
 
}
interface WakenUpListener {
 public void ActionToWakenUp(WakenUpEvent wakenUpEvent);
}
class Dad implements WakenUpListener{
 
 public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {
  System.out.println("feed food");
  
 }
 
}
class GrandFather implements WakenUpListener{
 public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {
  System.out.println("hug child");
  
 }
}
 
oberser.properties中的内容是:
observers=com.yqs.dp.observer.Dad,com.yqs.dp.observer.GrandFather,com.yqs.dp.observer.Dog