Java Watcher pattern

Source: Internet
Author: User

1. "Push" mode, the viewer will update the data "push" to the Observer

Observableclassa inherits Observable. When data changes, that is, when the SetData () method is called, the value of the change is notified to the observer.

Observableclassa.java

1 Importjava.util.Observable;2 3 4  Public classObservableclassaextendsobservable{5     PrivateString data;6     7      PublicObservableclassa (String datainput) {8data =Datainput;9     }Ten      One      PublicString GetData () { A         return  This. Data; -     } -      the      Public voidSetData (String datainput) { -          This. data =Datainput; -          -          This. setchanged (); +          This. Notifyobservers ( This. data); -     } +}

The OBSERVERX implements the Observer interface, the update () method. When the observer broadcasts (notifies the observer), the Update method is called to print the updated data.

Observerx.java

1 Importjava.util.Observable;2 ImportJava.util.Observer;3 4 5  Public classObserverxImplementsobserver{6 7     PrivateString name;8     9      PublicObserverx (String namein) {TenName =Namein; One     } A      -      Public voidUpdate (Observable arg0, Object arg1) { -         if(arg0instanceofObservableclassa) { theString data =(String) arg1; -System.out.println ("Observer" + name + ": Data updated, value is" +data); -         } -     } +  -}

Ovserverexample.java

1  Public classObserverexample {2 3      Public Static voidMain (String args[]) {4Observableclassa AClass =NewObservableclassa ("abc"));5Observerx x1 =NewObserverx ("X1");6Observerx x2 =NewObserverx ("X2");7         8 aclass.addobserver (x1);9 Aclass.addobserver (x2);Ten  OneAclass.setdata ("AAA"); AAclass.setdata ("BBB"); -     } -}

Operation Result:

Observer X2:data updated, value is Aaaobserver x1:data updated, value was Aaaobserver x2:data updated, value is Bbbob Server X1:data updated, value is BBB

2. "Pull" mode, the observer only notifies the observer that the data is updated, the observer needs to "pull" the data to come over.

Observableclassa.java

1 Importjava.util.Observable;2 3 4  Public classObservableclassaextendsobservable{5     PrivateString data;6     7      PublicObservableclassa (String datainput) {8data =Datainput;9     }Ten      One      PublicString GetData () { A         return  This. Data; -     } -      the      Public voidSetData (String datainput) { -          This. data =Datainput; -          -          This. setchanged (); + //this.notifyobservers (this.data);//Push Mode -          This. Notifyobservers ();//Pull Mode +     } A}

Observerx.java

1 Importjava.util.Observable;2 ImportJava.util.Observer;3 4 5  Public classObserverxImplementsobserver{6 7     PrivateString name;8     9      PublicObserverx (String namein) {TenName =Namein; One     } A      -      Public voidUpdate (Observable arg0, Object arg1) { -         if(arg0instanceofObservableclassa) { theObservableclassa Observablea =(Observableclassa) arg0; -String data =Observablea.getdata (); -System.out.println ("Observer" + name + ": Data updated, value is" +data); -         } +     } -      + //Public void Update (Observable arg0, Object arg1) { A //if (arg0 instanceof Observableclassa) { at //String data = (string) arg1; - //System.out.println ("Observer" + name + ": Data updated, value is" + data); - //        } - //    } -}

Operation Result:

Observer X2:data updated, value is Aaaobserver x1:data updated, value was Aaaobserver x2:data updated, value is Bbbob Server X1:data updated, value is BBB

Java Watcher mode

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.