I have read the observer mode... I wrote it in C ++, and then I went back to Java. I was so happy to see the obserable.

Source: Internet
Author: User

Starting from the client:

@ Testpublic void testobserver () {numobserverable observable = new numobserverable (); // obobserver observer1 = new numobserver (); // observer observer2 = new oddoberser (); // observer observer3 = new evenoberser (); // observer observable. addobserver (observer2); // observable. addobserver (observer3); observable. addobserver (observer1); observable. setdata (5 );}

 

Numobserverab class

public class NumObserverable extends Observable{public static final Integer EVEN = 2;public static final Integer ODD = 1;private int data = 0;public int getData() {return data;}public void setData(int data) {this.data = data;Integer flag = EVEN;if((data&0x00001) == 1){flag = ODD;}setChanged();notifyObservers(flag);}}

Oddobserver class

Public class oddoberser implements observer {public void Update (observable o, object Arg) {If (ARG = numobserverable. odd) {system. out. println ("numoberserale has an odd number setting:" + (numobserverable) O ). getdata ());}}}

Evenobserver class

Public class evenoberser implements observer {public void Update (observable o, object Arg) {If (ARG = numobserverable. even) {system. out. println ("numoberserale has an even number setting:" + (numobserverable) O ). getdata ());}}}

 

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.