Detailed Java programming observer observer design pattern _java

Source: Internet
Author: User
Tags in domain

The Java language contains a number of direct support for design patterns, such as command mode, Agent mode, observer mode, and so on. Although the Java-provided support for these patterns is simple, it does not meet the more complex applications. But in the simple scene, the use of these classes can often be seen by the effect of the vertical bar. So, if you don't have any special needs, it's best to take advantage of these classes in Java.
Observer mode, also known as listening mode, observer mode, is one of the classic design patterns (a of GOF). In the Java language, the classes and interfaces supported for this pattern are mainly the following, all from the Java.beans package:

Java.beans.PropertyChangeListener (interface) 
Java.beans.PropertyChangeSupport (Class)
Java.beans.PropertyChangeEvent (Class)
Java.beans.PropertyChangeListener

This is an interface, and it is clear that all the classes that implement this interface are listener (or observer), which is interested in some of the changes that are being monitored. This interface is one method:

public void PropertyChange (Propertychangeevent evt) {  
//TODO auto-generated method stub  
} 

The interface definition is simple and the function is obvious. Accept an event (the propertychangeevent produced by the listener), and then do a little reaction based on the event.

 Java.beans.PropertyChangeSupport 

This class is used in the class of the observer to hold the registered observer and to provide them with information about the change of the observed person. This kind of method is not much, but still only introduce 100% use, or the brain is not enough to make, hehe.

Public Propertychangesupport (Object Sourcebean) 

This is the constructor, and the parameter is the listener. PropertyChangeListener is generally used as an attribute of the listener. Generally used as follows:

 
 


Note that this listeners is not just a listener, he may be a group of listeners. So how are these listeners? This time we use the following method.

public void Addpropertychangelistener (PropertyChangeListener listener) 

This class is too easy to add to the listener. Like the opening of the 17, reporters want to interview, you have to register first. Obviously this method can be called multiple times (add). There is a reduction in addition:

public void Removepropertychangelistener (PropertyChangeListener listener) 

If the listener is not interested in any changes to the listener, he is driven out by the listener.
Well, the reporters are here, the listener has changed to notify the others, using the following methods:

The public void Firepropertychange (propertychangeevent evt)  
 
is public void Firepropertychange (String PropertyName,  
                Boolean OldValue,  
                boolean newvalue) public  
 
void Firepropertychange (String propertyname,  
                int OldValue,  
                int newvalue)  
 
public void Firepropertychange (String propertyname,  
                object OldValue,  
                object NewValue) 

In fact, the parameters of the latter three methods are encapsulated into propertychangeevent, and then the first method is called. In practice, however, we prefer to directly call one of the last three, and we don't care about the encapsulation. The parameters of the last three methods are three, and the OldValue and newvalue are the values before and after the change, and the first is to change a name so that the listener responds according to the name. Like a meeting, all government information will be heard by reporters, but some journalists are only interested in the Taiwan issue, while some journalists are interested in China-Japan issues.
So much for the introduction of the Propertychangesupport method. Note that since Propertychangesupport is being used in the observer's class (typically a model), his methods are only invoked when observed here.

Java.beans.PropertyChangeEvent

This class I also lazy to introduce, see his main method to understand what's going on

Public String Getpropertyname () Public  
Object Getnewvalue () public object  
Getoldvalue () 

In the case of three categories, then there is a specific analysis of specific problems. For example, the first is the observed:

public class domain{protected String ID;  
  protected String name;  
 
  protected String desname;  
 
  Protected Propertychangesupport listeners = new Propertychangesupport (this);  
  Public String GetId () {return id;  
    public void SetId (String id) {this.id = ID;  
  Firepropertychange ("Domain.id", NULL, ID);  
  Public String Getdesname () {return desname;  
    } public void Setdesname (String desname) {this.desname = Desname;  
  Firepropertychange ("Domain.desname", null, desname);  
  Public String GetName () {return name;  
    public void SetName (String name) {this.name = name;  
  Firepropertychange ("Domain.name", null, name); } public void Addpropertychangelistener (PropertyChangeListener listener) {Listeners.addpropertychangelistener  
  (listener); } public void Firepropertychange (String propname, Object OldValue, Object newvalue) {Listeners.firepropertych Ange(PropName, OldValue, NewValue); } public void Removepropertychangelistener (PropertyChangeListener listener) {Listeners.removepropertychangeli  
  Stener (listener); 
 }  
}

Some of the three attributes in domain are interesting. Here is one of these people:

public class Simpleobserver implements PropertyChangeListener {  
    
  ....  
    
  @Override public 
  void PropertyChange (Propertychangeevent evt) {  
    if Evt.getpropertyname (). Equals (" Domain.name ")) {  
      //do some work  
    }  
  }  
    
} 

Here's a simple test class:

public class simpletest{public  
  static void Main (string[] args) {  
    Simpleobserver observer = new Simpleobserver ( );  
    Domain domain = new domain ();  
    Domain.addpropertychangelistener (Observer);  
    Domain.setname ("yangsq");  
    ......  
  }  
} 

Obviously, the execution of the PropertyChange method in Simpleobserver can be observed.

Related Article

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.