我注意到
some people编写bean,支持属性更改观察器模式.
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.io.Serializable; public class SampleBean implements Serializable { public static final String PROP_SAMPLE_PROPERTY = "sampleProperty"; private String sampleProperty; private PropertyChangeSupport propertySupport; public ChartBean() { propertySupport = new PropertyChangeSupport(this); } public String getSampleProperty() { return sampleProperty; } public void setSampleProperty(String value) { String oldValue = sampleProperty; sampleProperty = value; propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY,oldValue,sampleProperty); } public void addPropertyChangeListener(PropertyChangeListener listener) { propertySupport.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { propertySupport.removePropertyChangeListener(listener); } }
但是,由于Web应用程序的无状态,我记得读者观察者模式在网络MVC模式中并不常用.
在Web应用程序Java bean中遵循上述模式是一个很好的做法吗?