简体   繁体   English

我是否应该在Java bean中为Web应用程序添加对PropertyChangeSupport和PropertyChangeListener的支持?

[英]Should I add support for PropertyChangeSupport and PropertyChangeListener in a Java bean for a web application?

I noticed that some people write beans with support for the Property Change observer pattern. 我注意到有些人写bean支持Property Change观察者模式。

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. 但是,我记得由于Web应用程序的无状态特性,观察者模式并不常用于基于Web的MVC模式。

Is it a good practice to follow the above pattern in web application Java beans? Web应用程序 Java bean中遵循上述模式是一种好习惯吗?

To be honest only bother if you are actually going to need the feature. 老实说,如果你真的需要这个功能,那就太麻烦了。 Most web applications don't need PropertyChangeSupport . 大多数Web应用程序不需要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. 我只看到它被用于Swing应用程序。

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. Web应用程序中的典型bean是一个非常短暂的对象,准备为单个请求提供服务,然后转入void以进行垃圾回收。 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. 主要的问题是Web应用程序是我的自然并发和多用户,这不会让自己适应具有监听器和事件等的长寿对象。

1) Don't add property change support unless you know you will need it. 1)除非您知道需要,否则不要添加属性更改支持。

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. 2)如果你的bean只是一个只有getters / setters / equals / hashcode的值对象,那么考虑使用AOP框架(我喜欢Spring)用对象来包装用于实现属性更改事件/支持的建议。 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. 这样,您的bean就不会受到某些上下文(通常是UI)中需要的逻辑的影响,并且可能会在不同的上下文中发生变化。 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. 这是我在向特定应用程序的所有域bean添加属性更改支持时学到的一个教训 - 用户界面使用它,但它混淆了服务器团队(在那里没有使用),只是在没有使用它的噪音。

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. 我也同意,有时你不需要听各个属性,只要知道对象中的任何东西是否有变化就足够了。

PropertyChangeListener is of a rather poor design anyway - all that magic string comparison. PropertyChangeListener无论如何都是一个相当差的设计 - 所有魔术字符串比较。 Much better go for a simple models with ChangeListener (or similar) and bring together with composite models. 使用ChangeListener (或类似)的简单模型更好,并与复合模型结合使用。

Unless you are doing something interactive and COMETy, then it doesn't make a great deal of sense in a web application. 除非您正在做一些交互式和COMETy,否则它在Web应用程序中没有多大意义。 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. 您甚至可以像使用webapps一样编写桌面应用程序。 Any change (or series of changes) and sync the GUI. 任何更改(或一系列更改)并同步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. 此外,性能成本也会从重大变化的关键时间(例如打开窗口)转移到非关键时刻,在这段时间内您需要刻录循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM