Detail the Observer design pattern for Java programming

  • 2020-04-01 04:33:38
  • OfStack

  The Java language includes many direct support for design patterns, such as the command pattern, the agent pattern, the observer pattern, and so on. Although the support for these patterns provided by Java is simple, it is not suitable for more complex applications. But in simple scenarios, using these classes can often lead to a visual effect. So, if you don't have any special requirements, it's best to take advantage of these classes in Java.
              The Observer pattern, also known as the monitor pattern and the Observer pattern, is one of the classic design patterns. The main classes and interfaces supported for this pattern in the Java language are 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 obviously, all classes that implement this interface are listeners (or observers) that are interested in certain changes to the object being listened to. This interface has only one method:


public void propertyChange(PropertyChangeEvent evt) {  
// TODO Auto-generated method stub  
} 

The interface definition is simple and the effect is obvious. Take an event (the PropertyChangeEvent generated by the listener) and do some reactions based on that event.


 java.beans.PropertyChangeSupport 

                This class is used in the observed's class to hold registered observers and to provide them with information about the observed's changes. This class method is not much, but still only introduce 100% used, or the brain is not enough, ha ha.


public PropertyChangeSupport(Object sourceBean) 

          This is the constructor, and the argument is the listener. PropertyChangeListener is typically a property of the listener. Generally used as follows:


private PropertyChangeSupport listeners = new PropertyChangeSupport(this); 

 
          Note that the listeners do not represent just one listener; they may be a group of listeners. So how are these listeners? Now I'm going to use the following method.


public void addPropertyChangeListener(PropertyChangeListener listener) 

              This class is too easy, add the listener. Just like the 17th national congress of the communist party of China, journalists who want to do interviews have to register first. Obviously this method can be called multiple times (add). Plus is minus:


public void removePropertyChangeListener(PropertyChangeListener listener) 

If the listener is no longer interested in any changes in the listener, the listener throws him out.
All right, now that the reporters are here, it's time to let them know if there's a change, using one of the following methods:


public void firePropertyChange(PropertyChangeEvent evt)  
 
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 arguments for the last three methods are all encapsulated as PropertyChangeEvent, and the first method is called. In practice, though, we prefer to call one of the last three directly and leave the encapsulation to ourselves. The last three methods take three arguments, oldValue and newValue are the values before and after the change, and the first one is to change the name so that listeners can respond according to the name. Just like a meeting, all the information of the government will be heard by the reporters, but some reporters are only interested in the Taiwan issue, while others are interested in the sino-japanese issue.
So much for the PropertyChangeSupport method. Note that since PropertyChangeSupport is used in the observed class (typically a model), its methods are called only in the observed class.


java.beans.PropertyChangeEvent

            I don't want to introduce this class, just look at his main methods and see what's going on


public String getPropertyName()  
public Object getNewValue()  
public Object getOldValue() 

On the three categories, there is a specific problem specific analysis. Here's an example. First, 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.firePropertyChange(propName, oldValue, newValue);  
  }  
 
  public void removePropertyChangeListener(PropertyChangeListener listener) {  
    listeners.removePropertyChangeListener(listener);  
  }  
} 

Someone is interested in the three properties of Domain. Here's one of them:


public class SimpleObserver implements PropertyChangeListener {  
    
  ....  
    
  @Override 
  public void propertyChange(PropertyChangeEvent evt) {  
    if(evt.getPropertyName().equals("Domain.name")){  
      //do some work  
    }  
  }  
    
} 

Here is 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, you can observe the execution of the propertyChange method in the SimpleObserver.


Related articles: