标签:style blog color java 数据 ar div log amp
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 = dataInput; 9 } 10 11 public String getData() { 12 return this.data; 13 } 14 15 public void setData(String dataInput) { 16 this.data = 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 = (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 AAA
Observer X1 : data updated, value is AAA
Observer X2 : data updated, value is BBB
Observer 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 = dataInput; 9 } 10 11 public String getData() { 12 return this.data; 13 } 14 15 public void setData(String dataInput) { 16 this.data = 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 = 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 = (String) arg1; 24 // System.out.println("Observer " + name + " : data updated, value is " + data); 25 // } 26 // } 27 }
运行结果:
Observer X2 : data updated, value is AAA
Observer X1 : data updated, value is AAA
Observer X2 : data updated, value is BBB
Observer X1 : data updated, value is BBB
标签:style blog color java 数据 ar div log amp
原文地址:http://www.cnblogs.com/ligh/p/3934755.html