Java-马士兵设计模式学习笔记-观察者模式-读取properties文件,动态增加观察者

一、概述

1.目标:用observer.properties文件存储observer类,当要增加observer时,无需修改代码,修改properties文件即可

2.properties文件位置的关系:当properties文件放在src目录下时,由于编译会自动把src里的文件放到bin文件平,因此可用this.getClass.getClassLoader.getResourceAsStream(fileName)读取,当把properties文件放到包里时,则应加相应的包路径,如:

props.load(Test.class.getClassLoader().getResourceAsStream("com/tong/observer/observer.properties"));

PS:由于每次都要load properties文件(IO操作耗资源),所以效率低,可考虑把相关IO操作的写成static块(单例模式),在类装载时只load一次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 

 88 public class Test {

 89 

 90     public static void main(String[] args) {

 91         

 92         Child c = new Child();

 93         

 94         Properties props = new Properties();

 95         try {

 96             props.load(Test.class.getClassLoader().getResourceAsStream("com/tong/observer/observer.properties"));

 97         } catch (Exception e) {

 98             e.printStackTrace();

 99         }

100         

101         String [] observers = props.getProperty("observers").split(",");

102         

103         for(String s : observers){

104             try {

105                 c.addWakenUpListener((WakenUpListener)Class.forName(s).newInstance());

106             } catch (Exception e) {

107                 e.printStackTrace();

108             }

109         }

110         new Thread(c).start();

111     }

112 }

 

2.observer.properties

 1 observers=com.tong.observer.Dad,com.tong.observer.GrandFather 

三、运行结果:

你可能感兴趣的:(properties)