Java-马士兵设计模式学习笔记-观察者模式-读取properties文件改成单例模式

一、概述

1.目标:读取properties文件改成单例模式

 

二、代码

1.Test.java

  1 class WakenUpEvent{
  2     
  3     private long time;
  4     private String location;
  5     private Child source;
  6     
  7     public WakenUpEvent(long time, String location, Child source) {
  8         super();
  9         this.time = time;
 10         this.location = location;
 11         this.source = source;
 12     }
 13 
 14     public long getTime() {
 15         return time;
 16     }
 17 
 18     public void setTime(long time) {
 19         this.time = time;
 20     }
 21 
 22     public String getLocation() {
 23         return location;
 24     }
 25 
 26     public void setLocation(String location) {
 27         this.location = location;
 28     }
 29 
 30     public Child getSource() {
 31         return source;
 32     }
 33 
 34     public void setSource(Child source) {
 35         this.source = source;
 36     }
 37     
 38     
 39 }
 40 
 41 class Child implements Runnable {
 42     
 43     private List<WakenUpListener> wakenUpListeners = new ArrayList<WakenUpListener>();
 44     
 45     public void addWakenUpListener(WakenUpListener wul){
 46         wakenUpListeners.add(wul);
 47     }
 48     public void wakeUp(){
 49         for(int i = 0; i < wakenUpListeners.size(); i++){
 50             WakenUpListener l = wakenUpListeners.get(i);
 51             l.actionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));
 52         }
 53     }
 54 
 55     @Override
 56     public void run() {
 57         try {
 58             Thread.sleep(3000);
 59         } catch (Exception e) {
 60             e.printStackTrace();
 61         }
 62         wakeUp();
 63     }
 64 }
 65 
 66 
 67 interface WakenUpListener {
 68     public void actionToWakenUp(WakenUpEvent e);
 69 }
 70 
 71 class Dad implements WakenUpListener {
 72 
 73     public void actionToWakenUp(WakenUpEvent e) {
 74         System.out.println("Fedd the child");
 75     }
 76     
 77 }
 78 
 79 class GrandFather implements WakenUpListener {
 80 
 81     public void actionToWakenUp(WakenUpEvent e) {
 82         System.out.println("抱孩子");
 83     }
 84     
 85 }
 86 
 87 class PropertiesMgr{
 88     
 89     private static Properties props = new Properties();
 90     
 91     //只加载一次,但如果properties有更新,它不会反馈
 92     static {
 93         try {
 94             props.load(Test.class.getClassLoader().getResourceAsStream("com/tong/observer/observer.properties"));
 95         } catch (Exception e) {
 96             e.printStackTrace();
 97         }
 98     }
 99 
100     public static String getProperty(String key){
101         return  props.getProperty(key);
102     }
103 }
104 public class Test {
105 
106     public static void main(String[] args) {
107         
108         Child c = new Child();
109         
110         String [] observers = PropertiesMgr.getProperty("observers").split(",");
111         
112         for(String s : observers){
113             try {
114                 c.addWakenUpListener((WakenUpListener)Class.forName(s).newInstance());
115             } catch (Exception e) {
116                 e.printStackTrace();
117             }
118         }
119         new Thread(c).start();
120     }
121 }

 

三、运行结果

posted @ 2015-06-19 16:13  shamgod  阅读(427)  评论(0编辑  收藏  举报
haha