我是否应该在 Web 应用程序的 Java bean 中添加对 PropertyChangeSupport 和 PropertyChangeListener 的支持?
我注意到有些人编写了支持属性更改观察者模式的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 应用程序的无状态性质,观察者模式在基于 Web 的 MVC 模式中并不常用。
在Web应用程序 Java bean中遵循上述模式是一个好的实践吗?
I noticed that some people write beans with support for the Property Change observer pattern.
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);
}
}
However, I remember reading that observer pattern is not commonly used in web based MVC patterns, due to the stateless nature of web applications.
Is it a good practice to follow the above pattern in web application Java beans?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
老实说,只有当您确实需要该功能时才需要麻烦。 大多数 Web 应用程序不需要
PropertyChangeSupport
。 我实际上不记得在我见过的任何网络应用程序中看到过它。 我只见过它在 Swing 应用程序中使用。Web 应用程序中的典型
bean
是一个生命周期相当短的对象,准备为单个请求提供服务,然后丢弃到空白中以进行垃圾收集。 主要问题是,Web 应用程序本质上是并发和多用户,这不适合具有侦听器和事件等的寿命较长的对象。To be honest only bother if you are actually going to need the feature. Most web applications don't need
PropertyChangeSupport
. I can't actually remember seeing it being used in any web app that I've seen. I've only seen it being used a Swing application.A typical
bean
in a web application is a pretty short lived object, prepared to service the single request and then cast off in to the void to be garbage collected. The main issue is that web applications are my there nature concurrent and multi user this doesn't lend it self to longer lived objects with listeners and events etc.无论如何,PropertyChangeListener 的设计相当糟糕 - 所有这些神奇的字符串比较。 最好选择带有
ChangeListener
(或类似)的简单模型,并将其与复合模型结合在一起。除非您正在做一些交互式和 COMETy 的事情,否则它在 Web 应用程序中没有多大意义。 您通常有一个拉模型,其中所有当前信息都一次性捆绑在一起。 如果你有缓存,这可能是有意义的。
您甚至可以像编写 Web 应用程序一样编写桌面应用程序。 任何更改(或一系列更改)并同步 GUI。 事实证明这非常紧凑。 此外,性能成本也从重大变更的关键时间(例如打开窗口)转移到了需要消耗周期的非关键时间。
PropertyChangeListener
is of a rather poor design anyway - all that magic string comparison. Much better go for a simple models withChangeListener
(or similar) and bring together with composite models.Unless you are doing something interactive and COMETy, then it doesn't make a great deal of sense in a web application. You generally have a pull model where all the current information is bundled up in one go. It may make sense where you have caches.
You can even write desktop applications in the same manner as webapps. Any change (or series of changes) and sync the GUI. This turns out to be quite compact. Also the performance costs are moved from the critical time of major changes (such as opening a window) to be spread over non-critical time where you have cycles to burn.
1) 除非您知道需要,否则不要添加属性更改支持。
2)如果你的bean只是一个值对象,除了getters/setters/equals/hashcode之外什么都没有,那么考虑使用AOP框架(我喜欢Spring)来包装该对象,并使用用于实现属性更改事件/支持的建议。 这样,您的 bean 就不会受到仅在某些上下文(通常是 UI)中需要的逻辑的污染,并且在不同的上下文中可能会发生变化。 这是我在为特定应用程序的所有域 bean 添加属性更改支持时学到的教训 - UI 使用了它,但它使服务器团队感到困惑(在那里没有使用),并且在没有使用它的地方只是噪音。
我也同意,有时您不需要监听各个属性,知道对象中的任何内容是否发生变化就足够了。
1) Don't add property change support unless you know you will need it.
2) If your bean is just a value object with nothing much more than getters/setters/equals/hashcode, then consider using an AOP framework (I like Spring) to wrap the object with advices used to implement property change events/support. This way your bean stays unpolluted with logic that is only needed in certain contexts (usually the UI) and that might change in different contexts. This is a lesson I learned when I added property change support to all the domain beans for a particular app - the UI used it, but it confused the server team (not used there) and was just noise where it wasn't used.
I also agree that at times you don't need to listen to individual properties, it is enough to know if anything in the object has changed.