Java: Application of observer Interface Practice Observer mode

Source: Internet
Author: User

This article is from the "sub-larvae" blog, the original link: http://zhangjunhd.blog.51cto.com/113473/68949

The Observer pattern is implemented in Java through the observable class and the Observer interface. The Observer object is the observer, and the observable object is the observer.

1. implementing the Observer patternImplementing the Observer pattern is very simple, [1] creates the Observer class, which inherits from the Java.util.Observable class, [2] creates the Observer class, it implements the Java.util.Observer interface, and [3] for the Observer class, adds its observer:
void addobserver (Observer o)

 The Addobserver () method adds the Observer object to the list of observer objects. When the observed event occurs, execute:
Setchanged (); Notifyobservers ();

only after Setchange () is called, Notifyobservers () will call Update (). the Setchange () method is used to set an internal flag bit indicating that the data has changed, and the Notifyobservers () method calls the update () method of all observer in the Observer object list to notify them that the data has changed.

[4] for the Observer class, the only way to implement the Observer Interface update
void Update (Observable o, Object Arg)

2. Instance one parameter arg, corresponding to one by Notifyobservers (object arg), passed parameter, when executed is notifyobservers (); ARG is null.

Numobserable is an observer who notifies all observers when the value of its member variable data changes.Numobserable.java
 PackageCom.zj.observer;Importjava.util.Observable; Public classNumobservableextendsObservable {Private intdata = 0;  Public intGetData () {returndata; }      Public voidSetData (inti) {data=i;       Setchanged ();    Notifyobservers (); }}

Numobserver is the Observer. When its observer (numobserable) executes notifyobservers (), it executes the uodate () method.  

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 have changed to" +myobserable.getdata ()); }}

The test class Singletest, where the observer is added to the Observer's watch list.  

Singletest.java
Package publicclass  singletest {    publicstatic   void  Main (string[] args) {       new  numobservable ();       Number.addobserver (new  numobserver ());       Number.setdata (1);       Number.setdata (2);       Number.setdata (3);}    }

Results:

Data have changed to 1

Data have changed to 2Data have changed to 3 3. Example TwoIn this example, the data is observed, with two observers, observing the change of odd and even numbers, and identifying the notification information by the parameter arg in Notifyobservers (ARG).Numsobservable.java by the Observer
 Packagecom.zj.observers;Importjava.util.Observable; Public classNumsobservableextendsObservable { Public Final StaticInteger ODD = 1;  Public Final StaticInteger even = 2; Private intdata = 0;  Public intGetData () {returndata; }      Public voidSetData (inti) {data=i; Integer Flag=even; if(Data & 0x0001) = = 1) Flag=ODD;       Setchanged ();    Notifyobservers (flag); }}

Odd Observer Oddobserver.java  

 Packagecom.zj.observers;Importjava.util.Observable;ImportJava.util.Observer; Public classOddobserverImplementsObserver { Public voidUpdate (Observable o, Object Arg) {if(arg = =numsobservable.odd) {numsobservable myobserable=(numsobservable) o; System.out.println ("Oddobserver:data have changed to" +myobserable.getdata ()); }    }}

even observers Evenobserver.java  

 Packagecom.zj.observers;Importjava.util.Observable;ImportJava.util.Observer; Public classEvenobserverImplementsObserver { Public voidUpdate (Observable o, Object Arg) {if(arg = =numsobservable.even) {numsobservable myobserable=(numsobservable) o; System.out.println ("Evenobserver:data have changed to" +myobserable.getdata ()); }    }}

Test Class Multitest.java  

 package   com.zj.observers;  public  class   Multitest { static  void   main (string[] args) {Numsob       Servable number  = new   numsobservable (); Number.addobserver ( new   Oddobserver ())       ; Number.addobserver ( new   Evenobserver ()       );       Number.setdata ( 1);       Number.setdata ( 2);    Number.setdata ( 3); }}

Oddobserver:data have changed to 1 Results:

Evenobserver:data have changed to 2oddobserver:data have changed to 3

Java: Application of observer Interface Practice Observer mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.