《Java设计模式 -- Observer》
import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Properties;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 Child implements Runnable {private List<WakenUpListener> wakenUpListeners = new ArrayList<WakenUpListener>();public void addWakenUpListener(WakenUpListener l) {wakenUpListeners.add(l);}void wakeUp() {for(int i=0; i<wakenUpListeners.size(); i++) {WakenUpListener l = wakenUpListeners.get(i);l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), "bed", this));}}public void run() {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}this.wakeUp();}}class Dad implements WakenUpListener {public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {System.out.println("feed child");}}class GrandFather implements WakenUpListener {public void ActionToWakenUp(WakenUpEvent wakenUpEvent) {System.out.println("hug child");}}class Dog implements WakenUpListener {public void ActionToWakenUp(WakenUpEvent arg0) {System.out.println("wang wang ...");}}interface WakenUpListener {public void ActionToWakenUp(WakenUpEvent wakenUpEvent);}public class Test {public static void main(String[] args) {Child c = new Child();String[] observers = PropertyMgr.getProperty("observers").split(",");for(String s : observers) {try {c.addWakenUpListener((WakenUpListener)(Class.forName(s).newInstance()));} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}new Thread(c).start();}}class PropertyMgr {private static Properties props = new Properties();static {try {props.load(Test.class.getClassLoader().getResourceAsStream("com/bjsxt/dp/observer/observer.properties"));} catch (IOException e) {e.printStackTrace();}}public static String getProperty(String key) {return props.getProperty(key);}}class CryEvent {}abstract class Event {}
?
?
利用java的现有方法
?
import java.util.Observable;import java.util.Observer;public class PriceObserver implements Observer { private float price = 0; public void update(Observable obj, Object arg) { if (arg instanceof Float) { price = ((Float) arg).floatValue(); System.out.println("PriceObserver :price changet to " + price); } }}
?
import java.util.Observable;public class Product extends Observable { private float price; public float getPrice() { return price; } public void setPrice(float price) { this.price = price; //设置变化点 setChanged(); notifyObservers(new Float(price)); }}
?
public class Test { public static void main(String[] args) { Product product = new Product(); PriceObserver priceobs = new PriceObserver();//加入观察者 product.addObserver(priceobs); product.setPrice(9.22f); }}
?