Design Mode-behavior-Observer Mode)

Source: Internet
Author: User

Overview
Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated.
What is most easily linked is the correspondence between data and views on the GUI. When the data changes, all the views corresponding to the GUI are automatically updated.
Applicability
1. When an abstract model has two aspects, one of which depends on the other.
Encapsulate the two in independent objects so that they can be changed and reused independently.

2. When changing an object, you need to change other objects at the same time without knowing how many objects need to be changed.

3. When an object must notify other objects, it cannot assume who the other objects are.

Participants
1. Subject (target)
The target knows its observer. Multiple observers can observe the same object.
Provides interfaces for registering and deleting observer objects.

2. Observer (Observer)
Defines an update interface for objects that need to be notified when the target changes.

3. ConcreteSubject (target)
Save the status to the ConcreteObserver objects.
When its status changes, a notification is sent to its various observers.

4. ConcreteObserver (Specific observer)
Maintain a reference pointing to the ConcreteSubject object.
Storage status, which must be consistent with the target status.
Implement the Observer update interface to make its status consistent with that of the target.

Class Diagram

Example
Package com. sql9.actioned;
 
Import java. util. ArrayList;
Import java. util. List;
 
 
/**
* Observer mode example
* @ Author iihero
*
*/
 
Abstract class Subject {
Private List <Observer> list = new ArrayList <Observer> ();
Private String data = "Initial ";
Public void setData (String data ){
This. data = data;
}
Public String getData (){
Return data;
}
Public void attach (Observer o ){
List. add (o );
O. setSubject (this );
} Www.2cto.com
Public void detach (Observer o ){
List. remove (o );
O. setSubject (null );
}
Public abstract void setState (String state );
Public abstract String getState ();
Public void doNotify (){
For (Observer o: list ){
O. update ();
}
}
}
 
Class ConcreteSubject extends Subject {
String state;

@ Override
Public void setState (String state ){
This. state = state;
}
 
@ Override
Public String getState (){
Return state;
}

}
 
Abstract class Observer {
Protected Subject s;
Public Subject getSubject (){
Return s;
}
Public void setSubject (Subject s ){
This. s = s;
}
Public abstract void update ();
}
 
Class ConcreteObserver extends Observer {
Private String observerName;
Public ConcreteObserver (String name ){
This. observerName = name;
}
@ Override
Public void update (){
System. out. println ("In Observer" + this. observerName + "data from subject:" + this. s. getData ());
}
 

}
 
 
 
Public class ObserverTest {
 
Public static void main (String [] args ){
ConcreteSubject s = new ConcreteSubject ();
ConcreteObserver o1 = new ConcreteObserver ("first observer ");
ConcreteObserver o2 = new ConcreteObserver ("second observer ");
ConcreteObserver o3 = new ConcreteObserver ("second observer ");
 
S. attach (o1 );
S. attach (o2 );
S. attach (o3 );
 
// Make changes to subject
S. setData ("This is important subject data ");
 
// Configure y all observers
S. donostrap ();
}
 
}


Result
In Observer first observer data from subject: This is important subject data
In Observer second observer data from subject: This is important subject data
In Observer second observer data from subject: This is important subject data

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.