Java Observer设计模式
1 package observer; 2 //接口,用以实现小孩醒来时要做的事 3 public interface IWakeupListener { 4 5 public void ActionToWakeup(WakeupEvent eve); 6 }
1 package observer; 2 //Dad实现喂奶的动作 3 public class Dad implements IWakeupListener{ 4 @Override 5 public void ActionToWakeup(WakeupEvent e) { 6 System.out.println("儿子醒了,我要去喂奶了。。。"); 7 } 8 }
1 package observer; 2 //狗实现叫的动作 3 public class Dog implements IWakeupListener { 4 5 @Override 6 public void ActionToWakeup(WakeupEvent e) { 7 System.out.println("小主人醒了,我就汪汪叫。。。"); 8 } 9 10 }
1 package observer; 2 //爷爷实现抱的动作 3 public class GrandFather implements IWakeupListener{ 4 5 @Override 6 public void ActionToWakeup(WakeupEvent e) { 7 System.out.println("小孙子醒了,我要去抱抱!!!"); 8 } 9 }
1 package observer; 2 //小孩的醒来事件:醒来时间,醒来地点,醒来的是谁 3 public class WakeupEvent { 4 private long time; 5 private String location; 6 private Child sorce; 7 8 9 public WakeupEvent(long time, String location, Child sorce) { 10 super(); 11 this.time = time; 12 this.location = location; 13 this.sorce = sorce; 14 } 15 public long getTime() { 16 return time; 17 } 18 public void setTime(long time) { 19 this.time = time; 20 } 21 public String getLocation() { 22 return location; 23 } 24 public void setLocation(String location) { 25 this.location = location; 26 } 27 public Child getSorce() { 28 return sorce; 29 } 30 public void setSorce(Child sorce) { 31 this.sorce = sorce; 32 } 33 }
1 package observer; 2 //小孩 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 public class Child implements Runnable{ 8 9 private List<IWakeupListener> listener = new ArrayList<IWakeupListener>(); 10 11 public void addWakeupListener(IWakeupListener l) 12 { 13 listener.add(l); 14 } 15 16 //-------------测试 17 @Override 18 public void run() { 19 try { 20 Thread.sleep(3000); 21 } catch (InterruptedException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 wakeup(); 26 } 27 28 private void wakeup() { 29 // Iterator<IWakeupListener> iter = listener.iterator(); 30 // while (iter.hasNext()) { 31 // IWakeupListener l = iter.next(); 32 // } 33 34 for(Iterator<IWakeupListener> iter = listener.iterator();iter.hasNext();) 35 { 36 IWakeupListener l = iter.next(); 37 l.ActionToWakeup(new WakeupEvent(System.currentTimeMillis(), "home", this)); 38 } 39 40 } 41 //-------------测试end 42 43 }
配置文件:observer.properties,放在asset/config文件夹下
1 observers=observer.Dad;observer.GrandFather;observer.Dog
1 package observer; 2 //解析.properties 3 import java.io.IOException; 4 import java.util.Properties; 5 6 public class PropertyProxy { 7 8 private Properties properties = new Properties(); 9 private static PropertyProxy instance=null; 10 public static PropertyProxy Instance() 11 { 12 if(instance==null) 13 { 14 instance = new PropertyProxy(); 15 } 16 return instance; 17 } 18 19 public String getProperty(String path,String key) 20 { 21 try { 22 properties.load(PropertyProxy.class.getClassLoader().getResourceAsStream(path)); 23 } catch (IOException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 28 return properties.getProperty(key); 29 } 30 }
1 package observer; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 public class Child implements Runnable{ 8 9 private List<IWakeupListener> listener = new ArrayList<IWakeupListener>(); 10 11 public void addWakeupListener(IWakeupListener l) 12 { 13 listener.add(l); 14 } 15 16 //-------------测试 17 @Override 18 public void run() { 19 try { 20 Thread.sleep(3000); 21 } catch (InterruptedException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 wakeup(); 26 } 27 28 private void wakeup() { 29 // Iterator<IWakeupListener> iter = listener.iterator(); 30 // while (iter.hasNext()) { 31 // IWakeupListener l = iter.next(); 32 // } 33 34 for(Iterator<IWakeupListener> iter = listener.iterator();iter.hasNext();) 35 { 36 IWakeupListener l = iter.next(); 37 l.ActionToWakeup(new WakeupEvent(System.currentTimeMillis(), "home", this)); 38 } 39 40 } 41 //-------------测试end 42 43 }
1 package observer; 2 3 import oracle.net.aso.c; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 Child child = new Child(); 9 10 String[] observerArr = PropertyProxy.Instance().getProperty("asset/config/observer.properties", "observers").split(";"); 11 12 for(String className: observerArr) 13 { 14 IWakeupListener l = null; 15 try { 16 l = (IWakeupListener)(Class.forName(className).newInstance()); 17 } catch (InstantiationException | IllegalAccessException 18 | ClassNotFoundException e) { 19 e.printStackTrace(); 20 } 21 child.addWakeupListener(l); 22 } 23 24 new Thread(child).start(); 25 } 26 }
I don't extravagant hope to change the world, only hope this world will not change me.