先举列子,对Observable有点感觉....
观察者为羊,被观察者为狼 模仿的场景为狼叫羊跑
代码如下:
1.被观察者类
package test.pattern.observer; import java.util.Observable; public class Wolf extends Observable{ private String name; Wolf(String name) { this.name = name; } public void cry(String state){ System.out.println(this.getName()+ " crying "); this.setChanged(); this.notifyObservers(state); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.观察者类
package test.pattern.observer; import java.util.Observable; import java.util.Observer; public class Sheep implements Observer { private String state = "eating"; private String name; public Sheep(String name){ this.name = name; } public void update(Observable o, Object arg) { // TODO Auto-generated method stub Wolf wolf = (Wolf)o; System.out.println(wolf.getName()+" crying and "+arg+" "+this.getName()+" running....."); setState("running"); } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3.测试
package test.pattern.observer; public class TestObserver { public static void main(String[] args) { Wolf wolf = new Wolf("wolf1"); Sheep sheep1 = new Sheep("sheep1"); Sheep sheep2 = new Sheep("sheep2"); Sheep sheep3 = new Sheep("sheep3"); //注册观察者,sheep1,sheep2加入,sheep3未加入 wolf.addObserver(sheep1); wolf.addObserver(sheep2); String wolfStat = "hungry"; //wolf begin cry wolf.cry(wolfStat); } }
4.结果:
wolf1 crying
wolf1 crying and hungry sheep2 running.....
wolf1 crying and hungry sheep1 running.....
------------------------------*****************-------------------------------
下面,再具体分析观察者模式每一块功能
------------------------------*****************-------------------------------
在Java中通过Observable类和Observer接口实现了观察者模式。Observer对象是观察者,Observable对象是被观察者。
1. 实现观察者模式
实现观察者模式非常简单,
[1]创建被观察者类,它继承自java.util.Observable类;
[2]创建观察者类,它实现java.util.Observer接口;
[3]对于被观察者类,
添加它的观察者:
void addObserver(Observer o)
addObserver()方法把观察者对象添加到观察者对象列表中。
当被观察事件发生时,执行:
setChanged();
notifyObservers();
setChange()方法用来设置一个内部标志位注明数据发生了变化;notifyObservers()方法会去调用观察者对象列表中
所有的Observer的update()方法,通知它们数据发生了变化。
只有在setChange()被调用后,notifyObservers()才会去调用update()。
[4]对于观察者类,实现Observer接口的唯一方法update
void update(Observable o, Object arg)
形参Object arg,对应一个由notifyObservers(Object arg);传递来的参数,
当执行的是notifyObservers();时,arg为null。
2.实例一
NumObserable是一个被观察者,当它的成员变量data的数值发生变化时,会通知所有的观察者。
NumObserable.java
package com.zj.observer; import java.util.Observable; public class NumObservable extends Observable { private int data = 0; public int getData() { return data; } public void setData(int i) { data = i; setChanged(); notifyObservers(); } }
NumObserver是观察者。当它的被观察者(NumObserable)执行了notifyObservers()后,它会执行uodate()方法。
NumObserver.java
package com.zj.observer; import java.util.Observable; import java.util.Observer; public class NumObserver implements Observer{ public void update(Observable o, Object arg) { NumObservable myObserable=(NumObservable) o; System.out.println("Data has changed to " +myObserable.getData()); } }
测试类SingleTest,在这里将观察者加入到被观察者的观察列表中。
SingleTest.java
package com.zj.observer; public class SingleTest { public static void main(String[] args) { NumObservable number = new NumObservable(); number.addObserver(new NumObserver()); number.setData(1); number.setData(2); number.setData(3); } }
结果:
Data has changed to 1
Data has changed to 2
Data has changed to 3
3.实例二
这个实例中,还是对data进行观察,拥有两个观察者,分别观察奇数和偶数的变化,
通过notifyObservers(arg)中的参数arg来识别通知信息。
被观察者NumsObservable.java
package com.zj.observers; import java.util.Observable; public class NumsObservable extends Observable { public final static Integer ODD = 1; public final static Integer EVEN = 2; private int data = 0; public int getData() { return data; } public void setData(int i) { data = i; Integer flag = EVEN; if ((data & 0x0001) == 1) flag = ODD; setChanged(); notifyObservers(flag); } }
奇数观察者OddObserver.java
package com.zj.observers; import java.util.Observable; import java.util.Observer; public class OddObserver implements Observer { public void update(Observable o, Object arg) { if (arg == NumsObservable.ODD) { NumsObservable myObserable = (NumsObservable) o; System.out.println("OddObserver:Data has changed to " + myObserable.getData()); } } }
偶数观察者EvenObserver.java
package com.zj.observers; import java.util.Observable; import java.util.Observer; public class EvenObserver implements Observer { public void update(Observable o, Object arg) { if (arg == NumsObservable.EVEN) { NumsObservable myObserable = (NumsObservable) o; System.out.println("EvenObserver:Data has changed to " + myObserable.getData()); } } }
测试类MultiTest.java
package com.zj.observers; public class MultiTest { public static void main(String[] args) { NumsObservable number = new NumsObservable(); number.addObserver(new OddObserver()); number.addObserver(new EvenObserver()); number.setData(1); number.setData(2); number.setData(3); } }
结果:
OddObserver:Data has changed to 1
EvenObserver:Data has changed to 2
OddObserver:Data has changed to 3
------------------------------------*********************----------------------------
转载:http://blog.csdn.net/dada360778512/article/details/6977758