Mode note-Observer Mode

Source: Internet
Author: User
Observer mode. I 've seen it before. I thought about it today. It seems a bit vague. Let's review it, and take notes to prepare ......

Behavior pattern. To implement this pattern, you should have these roles: Observed theme, observer. This is required! In general, further division of labor will be carried out to make building things available. Topic roles are further divided into abstract themes and specific themes, and observer roles are also divided into abstract observers and specific observer roles. This is the Subject, ConcreteSubject, Observer, and ConcreteObserver mentioned in this article.
According to the JAVA and mode book, the simplest two versions are implemented.
Version 1:
/** Abstract topic role, belonging to the Observer */
Public interface Subject {
/**
* Register a new observer object
*/
Public void attach (Observer observer );
/**
* Delete An observer object
*/
Public void detach (Observer observer );
/**
* Notify all registered observer objects
*/
Void policyobservers ();
}

/** The specific topic role, which belongs to the Observer */
Public class concretesubject implements subject {
Private vector observersvector = new vector ();
/** The implementation of several methods is omitted here. It is nothing more than adding observer to the vector */
/**
* Notify all registered observer objects
* Here is a little different from the book. I use the iterator interface to implement it.
* Enumeration is used in the book, but it is similar.
*/
Public void policyobservers (){
Iterator it = observersvector. iterator ();
While (it. hasnext ()){
(Observer) it. Next (). Update ();
}
}
}

/** Abstract observer role */
Public interface observer {
/**
* If you call this method, you will be updated.
*/
Void Update ();
}

/** Specific observer role */
Public class concreteobserver implements observer {
/**
* If you call this method, you will be updated.
*/
Public void Update (){
System. Out. println (This + "I am notified ");
}
}

The main () of the client is as follows:
Public static void main (string [] ARGs ){
Subject subject = new concretesubject ();
Observer observer1 = new concreteobserver ();
Observer observer2 = new concreteobserver ();
Subject. Attach (observer1 );
Subject. Attach (observer2 );
Subject. policyobservers ();
}

The implementation of version 1 is basically no different from that in the book, while version 2 is a little different. Version 2 only changes the details of the two observer in version 1. Paste it here:

/**
* This is a heavyweight abstract topic role. Move some sub-class methods to this
* Adds the weight of the abstract class and reduces the weight of the specific topic class.
*/
Public abstract class subject {
/**
* Save all references to the observer object
*/
Private vector observersvector = new vector ();

/**
* Change the topic status.
* This method does not exist in the book. To declare an object with a base class on the client, you can move the declaration of this method to this
*/
Public abstract void change (string newstate );

/** Here, the method for adding/deleting the observer object to or from the topic role is also omitted */

/**
* Notify all registered observer objects
*/
Public void policyobservers (){
Iterator it = observersvector. iterator ();
While (it. hasnext ()){
(Observer) it. Next (). Update ();
}
}
}

/**
* Lightweight and specific topic-type roles
*/
Public class ConcreteSubject extends Subject {

Private String state;

/**
* Change the topic status.
*/
Public void change (String newState ){
State = newState;
This. policyobservers ();
}
}
The client code is exactly the same.

In fact, such a model is relatively simple, of course, simple does not mean that the power is not great, haha. In another form:

Public class is observed {
Public void notification observer (){
Observer object. The event may occur ();
        }
    }

Public class observer {
Public void may occur (){
/* Do what you want to do here */
        }
    }

After the two classes are decomposed into abstract and specific classes, a relatively complete observer mode is implemented. Haha !~!~!~

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.