设计模式之Observer
这个模式可以参考awt包中的button极其相关事件的实现
说这个模式之前先用java简单的模拟一个场景吧(小孩在睡觉,醒了之后,爸爸要给他喂东西吃)。
先建两个类。爸爸这个类和小孩这个类,爸爸这个类主要提供一个负责向小孩喂东西的方法;小孩这个类主要提供一个哭的方法和一个什么时候哭的方法。
首先是爸爸这个类:Father.java
package com.yx.zzg.observer;public class Father {public void eatToChild(){System.out.println("eat to child....");}}package com.yx.zzg.observer;public class Child implements Runnable{private Father f;private boolean isweakup = false;public Child(Father f) {super();this.f = f;}@Overridepublic void run() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}weekUp();}public boolean weekUp() {f.eatToChild();return isweakup = true;}}package com.yx.zzg.observer;public class Test {public static void main(String[] args) {Child c = new Child(new Father());new Thread(c).start();}}package com.yx.zzg.observer;public class WeekUpEvent {private long time;private String location;private Object source;public WeekUpEvent(long time, String location, Object source) {super();this.time = time;this.location = location;this.source = source;}public long getTime() {return time;}public void setTime(long time) {this.time = time;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}public Object getSource() {return source;}public void setSource(Child source) {this.source = source;}}public boolean weekUp() {WeekUpEvent weekupEvent = new WeekUpEvent(System.currentTimeMillis(),"zhengzhou", this);if (weekupEvent.getLocation().equals("bj")) {f.eatToChild();}if (weekupEvent.getLocation().equals("hh")) {f.sing();}return isweakup = true;}package com.yx.zzg.observer;public interface WeekUpListener {public void ActionWeekUp(WeekUpEvent weekUpEvent);}package com.yx.zzg.observer;public class Father implements WeekUpListener {@Overridepublic void ActionWeekUp(WeekUpEvent weekUpEvent) {if (weekupEvent.getLocation().equals("bj")) {System.out.println("to eat....")}if (weekupEvent.getLocation().equals("hh")) {System.out.println("to sing.....")}}}package com.yx.zzg.observer;public class Child implements Runnable {WeekUpListener weekUpListener;public Child(WeekUpListener weekUpListener) {super();this.weekUpListener = weekUpListener;}@Overridepublic void run() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}weekUp();}public void weekUp() {weekUpListener.ActionWeekUp(new WeekUpEvent(System.currentTimeMillis(), "zhengzhou", this));}}package com.yx.zzg.observer;public class Test {public static void main(String[] args) {WeekUpListener wul=new Father();Child c = new Child(wul);new Thread(c).start();}}package com.yx.zzg.observer;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Child implements Runnable {private List<WeekUpListener> weekUpListeners = new ArrayList<WeekUpListener>();public void addWeekUpListener(WeekUpListener weekUpListener) {weekUpListeners.add(weekUpListener);}@Overridepublic void run() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}weekUp(new WeekUpEvent(System.currentTimeMillis(), "zhengzhou", this));}public void weekUp(WeekUpEvent weekUpEvent) {for (Iterator<WeekUpListener> iter = weekUpListeners.iterator(); iter.hasNext();) {WeekUpListener weekUpListener = iter.next();weekUpListener.ActionWeekUp(weekUpEvent);}}}package com.yx.zzg.observer;public class Test {public static void main(String[] args) {Child c=new Child();WeekUpListener wul = new Father();c.addWeekUpListener(wul);new Thread(c).start();}}package com.yx.zzg.observer;public class Test {public static void main(String[] args) {Child c = new Child();String[] str = PropertyMgr.getProperty("observers").split(",");for (String s : str) {try {c.addWeekUpListener((WeekUpListener) Class.forName(s).newInstance());} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}new Thread(c).start();}}