Java Java类
Java.util.Observable用于创建程序其他部分可以观察的子类。当此类子类的对象发生更改时,会通知观察类。 update()方法在观察者收到更改通知时被调用。
Note:
被观察的对象必须遵循两个简单的规则:
- 如果它被改变,它必须调用setChanged( )方法。
- 当它准备好通知观察者这一变化时,它必须调用notifyObservers()方法。这会导致调用观察对象中的update()方法。
Be careful, if the object calls notifyObservers( ) method without having previously called
setChanged( ) method, no action will take place.
被观察对象必须同时调用setChanged()和notifyObservers()方法,才能调用update() 。
Java.util.Observable 的构造函数:
- 可观察的( )
构造一个具有零个观察者的 Observable。
方法:
- addObserver(Observer observer) :将观察者添加到观察调用对象的对象列表中。
Syntax : public void addObserver(Observer observer) Exception : NullPointerException -> if the parameter observer is null
// Java code to demonstrate addObserver() method import java.util.*; // This is the observer class Observer1 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer1 is added"); } } // This is class being observed class BeingObserved extends Observable { void incre() { setChanged(); notifyObservers(); } } class ObserverDemo { // Driver method of the program public static void main(String args[]) { BeingObserved beingObserved = new BeingObserved(); Observer1 observer1 = new Observer1(); beingObserved.addObserver(observer1); beingObserved.incre(); } }
输出 :
Observer1 is added
- setChanged() :当调用对象改变时调用。
Syntax : protected void setChanged( ) Exception : NA.
// Java code to demonstrate setChanged() method import java.util.*; // This is first observer class Observer1 implements Observer { public void update(Observable obj, Object arg) { } } // This is class being observed class BeingObserved extends Observable { void func1() { setChanged(); System.out.println("Change status with setChanged :" + hasChanged()); notifyObservers(); } void func2() { System.out.println("Change status without setChanged :" + hasChanged()); notifyObservers(); } } class ObserverDemo { // Driver method of the program public static void main(String args[]) { boolean status; BeingObserved beingObserved = new BeingObserved(); Observer1 observer1 = new Observer1(); beingObserved.addObserver(observer1); beingObserved.func1(); beingObserved.func2(); } }
输出 :
Change status with setChanged :true Change status without setChanged :false
- clearChanged():表示这个对象不再改变,或者它已经通知了它的所有观察者它最近的改变,所以hasChanged()方法现在将返回false 。
Syntax : protected void clearChanged( ) Exception : NA
// Java code to demonstrate clearChanged() method import java.util.*; // This is the observer class Observer1 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Inside Observer1"); } } // This is the class being observed class BeingObserved extends Observable { void func1() { setChanged(); // clearChanged method removes all the changes made by setChanged method clearChanged(); notifyObservers(); } } class ObserverDemo { // Driver method of the program public static void main(String args[]) { BeingObserved beingObserved = new BeingObserved(); Observer1 observer1 = new Observer1(); beingObserved.addObserver(observer1); beingObserved.func1(); } }
输出 :
No Output
由于clearChanged( )方法已删除所有更改,因此未获得任何输出。
- notifyObservers() :通过调用update()通知调用对象的所有观察者它已更改。
将 null 作为第二个参数传递给update() 。Syntax : public void notifyObservers( ) Exception : NA
// Java code to demonstrate notifyObservers( ) method import java.util.*; // This is first observer class Observer1 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer1 Notified"); } } // This is second observer class Observer2 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer2 Notified"); } } // This is class being observed class BeingObserved extends Observable { void func1() { setChanged(); /*This method notifies the change to all the observers that are registered*/ notifyObservers(); } } class ObserverDemo { // Driver method of the program public static void main(String args[]) { BeingObserved beingObserved = new BeingObserved(); Observer1 observer1 = new Observer1(); Observer2 observer2 = new Observer2(); beingObserved.addObserver(observer1); beingObserved.addObserver(observer2); beingObserved.func1(); } }
输出 :
Observer2 Notified Observer1 Notified
- notifyObservers(Object obj) :通过调用update()通知调用对象的所有观察者它已更改。
obj作为参数传递给update( ) 。Syntax : public void notifyObservers(Object obj) Exception : NA
// Java code to demonstrate notifyObservers(Object obj) method import java.util.*; // This is first observer class Observer1 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer1 Notified with value : " + ((Integer)arg).intValue()); } } // This is second observer class Observer2 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer2 Notified with value : " + ((Integer)arg).intValue()); } } // This is class being observed class BeingObserved extends Observable { void func1() { setChanged(); /*This method notifies the change to all the observers that are registered and passes an object*/ notifyObservers(new Integer(10)); } } class ObserverDemo { // Driver method of the program public static void main(String args[]) { BeingObserved beingObserved = new BeingObserved(); Observer1 observer1 = new Observer1(); Observer2 observer2 = new Observer2(); beingObserved.addObserver(observer1); beingObserved.addObserver(observer2); beingObserved.func1(); } }
输出 :
Observer2 Notified with value : 10 Observer1 Notified with value : 10
- countObservers( ) :返回观察调用对象的对象数。
Syntax : public int countObservers( ) Returns : the number of observers of this object Exception : NA
// Java code to demonstrate countObservers() method import java.util.*; // This is first observer class Observer1 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer1"); } } // This is second observer class Observer2 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer2"); } } // This is class being observed class BeingObserved extends Observable { void func1() { setChanged(); notifyObservers(); } } class ObserverDemo { // Driver method of the program public static void main(String args[]) { BeingObserved beingObserved = new BeingObserved(); Observer1 observer1 = new Observer1(); Observer2 observer2 = new Observer2(); beingObserved.addObserver(observer1); beingObserved.addObserver(observer2); int count_observer = beingObserved.countObservers(); System.out.println("Number of observers is " + count_observer); beingObserved.func1(); } }
输出 :
Number of observers is 2 Observer2 Observer1
- deleteObserver(Observer observer):从观察调用对象的对象列表中删除观察者。
将null传递给此方法将无效。Syntax : public void deleteObserver(Observer observer) Exception : NA
// Java code to demonstrate deleteObserver(Observer observer) method import java.util.*; // This is first observer class Observer1 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer1"); } } // This is second observer class Observer2 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer2"); } } // This is class being observed class BeingObserved extends Observable { void func1() { setChanged(); notifyObservers(); } } class ObserverDemo { // Driver method of the program public static void main(String args[]) { int count_observer; BeingObserved beingObserved = new BeingObserved(); Observer1 observer1 = new Observer1(); Observer2 observer2 = new Observer2(); beingObserved.addObserver(observer1); beingObserved.addObserver(observer2); count_observer = beingObserved.countObservers(); System.out.println("Number of observers before" + " calling deleteObserver(): " + count_observer); beingObserved.func1(); // Deleting observer1 beingObserved.deleteObserver(observer1); count_observer = beingObserved.countObservers(); System.out.println("No. of observers after"+ " calling deleteObserver(): " + count_observer); beingObserved.func1(); } }
输出 :
Number of observers before calling deleteObserver(): 2 Observer2 Observer1 No. of observers aftercalling deleteObserver(): 1 Observer2
- deleteObservers() :删除调用对象的所有观察者。
Syntax : public void deleteObservers() Exception : NA
// Java code to demonstrate deleteObservers() method import java.util.*; // This is first observer class Observer1 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer1"); } } // This is second observer class Observer2 implements Observer { public void update(Observable obj, Object arg) { System.out.println("Observer2"); } } // This is class being observed class BeingObserved extends Observable { void func1() { setChanged(); notifyObservers(new Integer(10)); } } class ObserverDemo { // Driver method of the program public static void main(String args[]) { int count_observer; BeingObserved beingObserved = new BeingObserved(); Observer1 observer1 = new Observer1(); Observer2 observer2 = new Observer2(); beingObserved.addObserver(observer1); beingObserved.addObserver(observer2); count_observer = beingObserved.countObservers(); System.out.println("Number of observers before" + " calling deleteObserver(): " + count_observer); beingObserved.func1(); // Deleting all observers beingObserved.deleteObservers(); count_observer = beingObserved.countObservers(); System.out.println("No. of observers after "+ "calling deleteObserver(): " + count_observer); beingObserved.func1(); } }
输出 :
Number of observers before calling deleteObserver(): 2 Observer2 Observer1 No. of observers after calling deleteObserver(): 0
参考: Java中的 Observables