Observer mode-Observer

Source: Internet
Author: User

Keywords:Define one-to-multiple dependencies between objects. In this way, when an object changes its state, all objects dependent on it will receive notifications and automatically update
Definition:

Define one-to-multiple dependencies between objects. In this way, when an object changes its state, all objects dependent on it will receive notifications and automatically update

Role:

Topic (observability)
Observer

You can implement the observer mode on your own, or use the built-in JDK support. This is relatively simple:
Topic (observability): Java. util. observable class
Observer: Java. util. Observer Interface

How do I send notifications to a topic (observability?

1. inherit the java. util. observable class
2. The following two steps are required:
2.1 call the setchanged () method first to mark the fact that the status has changed
2.2 call one of the two policyobservers () methods.
Yyobservers ()
Notifyobservers (Object Arg)

How to change an object to an observer?
1. Each Observer must implement the java. util. Observer interface to make himself an observer.
2. Call addobserver () method

How do observers receive notifications?
Update (observable o, object OBJ) in the implemented observer.
Observable O: Let the observer know which topic to notify it.
Object OBJ: this is the data passed in by policyobservers ().

Example:

Observable and observer
I read the event frameworks of observable and observer some time ago. I think it's pretty good. Let's take a look at them first.
 
The observer interface and observable class can be used in Model-View Mode: when the data in the model changes, the model actively notifies the view of this change.
The model is implemented using the observable class, while the view can be implemented using the class that implements the observer interface. Example:
 
Import java. util. observable;
Import java. util. observer;

/**
* @ Author
* Observer
*
*/
Public class human implements observer {

Private string name;

Public Human (){
Super ();
}

Public Human (string name ){
Super ();

This. Name = Name;
}

Public void Update (observable o, object Arg ){

// Obtain the current status of the observed object
System. Out. println (invalidman) O). getstate ());

// Notify the current observer
System. Out. println (name + Arg );
}

}

Import java. util. observable;

/**
* Observer
**/
Public class invalidman extends observable {

Private int state;

// Difference
Public final static int state_bad = 0;

// Dead
Public final static int state_dead =-1;

// Good
Public final static int state_fine = 1;

Public invalidman (){
Super ();
}

Public invalidman (INT state ){
This. State = State;
}

Public int getstate (){
Return state;
}

Public void setstate (INT state ){
This. State = State;
}

Public void changestate (INT state ){
If (this. State! = State)
{
This. State = State;
This. setchanged ();

If (state_bad = state | state_dead = State)
{
This. policyobservers ("... dangerous ...");
}
Else if (state_fine = State)
{
This. policyobservers ("... prepare for discharge ...");
}
}
Else
{
This. policyobservers ("... old look ...");
}
}
}

Test demo import java. util. observer;

Public class observerdemo
{
Public observerdemo ()
{
}

Public static void main (string [] ARGs)
{
// Observer
Invalidman O1 = new invalidman (invalidman. state_bad );
// Observer
Observer OO1 = new human ("doctor ");
// Observer
Observer OO2 = new human ("nurse ");
// Observer
Observer oo3 = new human ("family ");

// Register an observer with the observed object
// Register an observer for a Patient: Doctor, nurse, or family member
O1.addobserver (OO1 );
O1.addobserver (OO2 );
O1.addobserver (oo3 );

// Change the status of the observed object
O1.changestate (invalidman. state_dead );
O1.changestate (invalidman. state_bad );
O1.changestate (invalidman. state_fine );
}

}

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.