Java 属性更改侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1264674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:51:41  来源:igfitidea点击:

Java Property Change listeners

java

提问by Jijoy

I would like to know , how the java beans property change listeners work. Do they use , EventListeners inside ? Is it good to use , property change listeners when we can do same with POJO implementation of mediator pattern . I mean performance wise ?

我想知道,java bean 属性更改侦听器是如何工作的。他们使用,EventListeners 里面吗?当我们可以对中介模式的 POJO 实现做同样的事情时,是否可以使用属性更改侦听器。我的意思是性能明智?

Thanks J

谢谢 J

回答by Kevin Day

The advantage is in the area of coupling. mediatorrequires that your classes be aware of the mediator (e.g. by passing the mediator instance into the constructor). Mediators can be very powerful, and can definitely provide more efficient notification - but at the cost of significant complexity.

优势在于耦合领域。 中介器要求您的类知道中介器(例如,通过将中介器实例传递给构造函数)。调解器可以非常强大,并且绝对可以提供更有效的通知 - 但以显着的复杂性为代价。

In contrast, the Observerdesign pattern (which JavaBeans property change listeners are an implementation of) are much easier to implement. But they can have interesting emergent behavior in complex systems.

相比之下,观察者设计模式(JavaBeans 属性更改侦听器是其实现)更容易实现。但它们可以在复杂系统中产生有趣的涌现行为。

For many situations, Observer is more than adequate (especially in the areas that JavaBeans tend to be used). In other situations, it is nowhere near adequate - a good example is in event propagation in the Glazed Lists library. They wound up having to use an EventPublisher (which is a mediator) to optimize event notification order, and ensure that dependencies are met before events propagate.

在很多情况下,Observer 已经绰绰有余(尤其是在 JavaBeans 倾向于使用的领域)。在其他情况下,它远远不够——一个很好的例子是Glazed Lists 库中的事件传播。他们最终不得不使用 EventPublisher(它是一个中介)来优化事件通知顺序,并确保在事件传播之前满足依赖关系。

So, the answer to your question is that this is not a matter of POJOs vs JavaBeans. It's a matter of which design pattern (Observer or Mediator) makes the most sense for a given use case. Both patterns focus on decoupling parts of the system from each other. And both patterns have situations where they are appropriate. For a huge number of situations, Observer is just fine - and is really, really easy to write.

因此,您的问题的答案是,这不是 POJO 与 JavaBeans 的问题。对于给定的用例,哪种设计模式(观察者或中介者)最有意义是一个问题。这两种模式都侧重于将系统的各个部分相互解耦。并且这两种模式都有适合的情况。对于大量情况,Observer 很好——而且真的非常容易编写。

I should also mention that the use of intermediate event objects is common in both Observer and Mediator implementations, so that's kind of a side issue. But modern VMs are really efficient at dealing with short lived objects like that - so it's really a non issue.

我还应该提到中间事件对象的使用在 Observer 和 Mediator 实现中很常见,所以这是一个附带问题。但是现代虚拟机在处理这样的短期对象方面非常有效 - 所以这真的不是问题。

回答by Aaron Digulla

The code you look for is in java.beans.PropertyChangeSupport. You use it like this:

您要查找的代码在java.beans.PropertyChangeSupport. 你像这样使用它:

protected transient PropertyChangeSupport changeSupport = new PropertyChangeSupport (this);

public void addPropertyChangeListener (String propertyName, PropertyChangeListener listener)
{
    changeSupport.addPropertyChangeListener (propertyName, listener);
}

public void removePropertyChangeListener (String propertyName, PropertyChangeListener listener)
{
    changeSupport.removePropertyChangeListener (propertyName, listener);
}

public void firePropertyChange (String propertyName, BigDecimal oldValue, BigDecimal newValue)
{
    if (oldValue != null && newValue != null && oldValue.compareTo (newValue) == 0) {
        return;
    }
    changeSupport.firePropertyChange(new PropertyChangeEvent(this, propertyName,
                                               oldValue, newValue));

}

The main advantage of using this API is that everyone is familiar with it. The main disadvantage is that the Beans API is pretty old, cumbersome from todays point of view and very limiting.

使用此 API 的主要优点是每个人都熟悉它。主要缺点是 Beans API 相当陈旧,从今天的角度来看很麻烦并且非常有限。

For example, you need the name of a property in many places. This either means you must copy a string (which breaks if you rename the property) or you must manually define a String constant for every field which is tedious.

例如,您在很多地方都需要一个属性的名称。这要么意味着您必须复制一个字符串(如果您重命名该属性会中断),要么您必须为每个繁琐的字段手动定义一个字符串常量。

The implementation itself is pretty fast and follows the Observer design pattern. Of course, there are other ways to implement this. The price would be that this is no longer a bean since it doesn't follow the API. Hence, you can't use your object in many frameworks without additional glue code.

实现本身非常快,并且遵循观察者设计模式。当然,还有其他方法可以实现这一点。代价是这不再是一个 bean,因为它不遵循 API。因此,如果没有额外的粘合代码,您就无法在许多框架中使用您的对象。