The Java language contains many direct support for design patterns, such as command mode, agent mode, observe mode, etc. Although Java provides support for these patterns is very simple and cannot meet more complex applications. But in simple scenarios, using these classes can often achieve the effect of seeing the shadow. Therefore, if you don’t have any special needs, it is better to use these classes in java.
Observer mode, also known as monitoring mode and observer mode, is one of the classic design modes (one of GOF). In the java language, there are mainly the following classes and interfaces supported for this pattern, 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. Obviously, all classes that implement this interface are listener (or observer), which will be interested in some changes in the object being listened to. This interface has one method:
public void propertyChange(PropertyChangeEvent evt) { // TODO Auto-generated method stub } The interface definition is simple and its function is obvious. Accept an event (PropertyChangeEvent generated by the listener), and then react based on this event.
java.beans.PropertyChangeSupport
This class is used in the class of the observer, to save registered observers and to provide them with the changes of the observer. There are not many methods for this type, but it is still only introduced 100% of them, otherwise you will not be able to use your brain, haha.
public PropertyChangeSupport(Object sourceBean)
This is the constructor, and the parameters are the listener. PropertyChangeListener is generally used as a property of the listener. Generally used as follows:
private PropertyChangeSupport listeners = new PropertyChangeSupport(this);
Note that this listener does not represent only one listener, he may be a group of listeners. So who are these listeners? This time I used 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, if a reporter wants to interview, he must register first. Obviously this method can be called multiple times (add). Addition and subtraction:
public void removePropertyChangeListener(PropertyChangeListener listener)
If the monitor is not interested in any changes in the monitored person, he will be driven out by the monitor.
Okay, all the reporters are here, and if there is any change in the monitor, it is time to notify the person. Use 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 parameters of the last three methods will be encapsulated into PropertyChangeEvent, and then the first method will be called. However, in reality, we still like to directly call one of the last three, and we don’t care about encapsulation. The last three methods have three parameters, among which oldValue and newValue are the values before and after. The first one is to change a name so that the listeners can respond based on this name. Just like in a meeting, all the government information will be heard by reporters, but some journalists are only interested in the Taiwan issue, while others are interested in the China-Japan issue.
That's all the introduction to the PropertyChangeSupport method. Note that since PropertyChangeSupport is used in the class (usually a model) of the observer, its methods are only called here when they are observed.
java.beans.PropertyChangeEvent
I'm too lazy to introduce this class, just look at his main method and understand what's going on
public String getPropertyName() public Object getNewValue() public Object getOldValue()
There are three categories, and the other is to analyze specific issues. Let’s take an example, first of all, the observer:
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); } } Some people are interested in the three attributes of Domain. 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 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, the execution of the propertyChange method in SimpleObserver can be observed.