首页 > 代码库 > Java观察者模式

Java观察者模式

1. “推” 模式,被观察者将更新的数据 “推” 给观察者们

ObservableClassA 继承 Observable。当data发生变化,即调用setData()方法时,将变化的数值通知给观察者们。

ObservableClassA.java

 1 import java.util.Observable; 2  3  4 public class ObservableClassA extends Observable{ 5     private String data; 6      7     public ObservableClassA(String dataInput) { 8         data =http://www.mamicode.com/ dataInput; 9     }10     11     public String getData() {12         return this.data;13     }14     15     public void setData(String dataInput) {16         this.data =http://www.mamicode.com/ dataInput;17         18         this.setChanged();19         this.notifyObservers(this.data);20     }21 }

ObserverX 实现 Observer接口,即update()方法。当被观察者发出广播(通知观察者)时,update方法被调用,打印更新数据。

ObserverX.java

 1 import java.util.Observable; 2 import java.util.Observer; 3  4  5 public class ObserverX implements Observer{ 6  7     private String name; 8      9     public ObserverX(String nameIn) {10         name = nameIn;11     }12     13     public void update(Observable arg0, Object arg1) {14         if (arg0 instanceof ObservableClassA) {15             String data =http://www.mamicode.com/ (String) arg1;16             System.out.println("Observer " + name + " : data updated, value is " + data);17         }18     }19 20 }

OvserverExample.java

 1 public class ObserverExample { 2  3     public static void main(String args[]) { 4         ObservableClassA aClass = new ObservableClassA("abc"); 5         ObserverX x1 = new ObserverX("X1"); 6         ObserverX x2 = new ObserverX("X2"); 7          8         aClass.addObserver(x1); 9         aClass.addObserver(x2);10 11         aClass.setData("AAA");12         aClass.setData("BBB");13     }14 }

运行结果:

Observer X2 : data updated, value is AAAObserver X1 : data updated, value is AAAObserver X2 : data updated, value is BBBObserver X1 : data updated, value is BBB

 

2. “拉” 模式,被观察者只通知观察者们数据有更新,观察者们需自己将数据 “拉” 过来。

ObservableClassA.java

 1 import java.util.Observable; 2  3  4 public class ObservableClassA extends Observable{ 5     private String data; 6      7     public ObservableClassA(String dataInput) { 8         data =http://www.mamicode.com/ dataInput; 9     }10     11     public String getData() {12         return this.data;13     }14     15     public void setData(String dataInput) {16         this.data =http://www.mamicode.com/ dataInput;17         18         this.setChanged();19 //        this.notifyObservers(this.data);//推模式20         this.notifyObservers();//拉模式21     }22 }

ObserverX.java

 1 import java.util.Observable; 2 import java.util.Observer; 3  4  5 public class ObserverX implements Observer{ 6  7     private String name; 8      9     public ObserverX(String nameIn) {10         name = nameIn;11     }12     13     public void update(Observable arg0, Object arg1) {14         if (arg0 instanceof ObservableClassA) {15             ObservableClassA observableA = (ObservableClassA) arg0;16             String data =http://www.mamicode.com/ observableA.getData();17             System.out.println("Observer " + name + " : data updated, value is " + data);18         }19     }20     21 //    public void update(Observable arg0, Object arg1) {22 //        if (arg0 instanceof ObservableClassA) {23 //            String data = http://www.mamicode.com/(String) arg1;>24 //            System.out.println("Observer " + name + " : data updated, value is " + data);25 //        }26 //    }27 }

运行结果:

Observer X2 : data updated, value is AAAObserver X1 : data updated, value is AAAObserver X2 : data updated, value is BBBObserver X1 : data updated, value is BBB

 

Java观察者模式