简体   繁体   English

实施有关财产变更通知的最佳方法是什么?

[英]What is the best way to implement notification about property change?

A have a bean (POJO-like) and want to make model for GUI component of it. 有一个bean(类似于POJO),并希望为其的GUI组件建立模型。 So I need to notify about each property change in order GUI component can reflect. 因此,我需要通知每个属性更改,以便GUI组件能够反映出来。

How to do this? 这个怎么做? Should I put notification and listener storage code just inside my bean? 我是否应该将通知和侦听器存储代码放在我的bean中? But this will make it "dirty". 但这会使它“肮脏”。 May be write some wrapper? 可能会写一些包装器? But this will duplicate getters and setters. 但这将重复获取器和设置器。

Are there any libraries and/or helper objects for this in Commons or somewhere else? 在Commons或其他地方是否有任何库和/或辅助对象?

UPDATE UPDATE

Also suppose I have AbstractList<E> implementation. 还要假设我有AbstractList<E>实现。 How to "propertize" it quickly, ie make it to notify listeners about changes? 如何快速“授权”它,即使其通知侦听器有关更改? For example, by firing ListDataEvent . 例如,通过触发ListDataEvent I know I can implement AbstractListModel<E> instead of AbstractList<E> but it looks worse. 我知道我可以实现AbstractListModel<E>而不是AbstractList<E>但是它看起来更糟。 I wish to stay mostly "pojo-like"... 我希望大部分时间保持“ pojo式” ...

Take a look at FXCollections (JDK 7): 看看FXCollections (JDK 7):

List<String> list = new ArrayList<String>();

list.add("s1");
list.add("s2");

ObservableList<String> observableList = FXCollections.observableList(list);

observableList.addListener(new ListChangeListener<String>() {
    @Override
    public void onChanged(Change<? extends String> change) {
        while (change.next()) {
            if(change.wasAdded()){
                System.out.printf("added: %s, from: %d, to: %d%n", change.getAddedSubList(), change.getFrom(), change.getTo());
            }else
            if(change.wasReplaced()){
                System.out.printf("replaced: %s, from: %d, to: %d%n", change.getAddedSubList(), change.getFrom(), change.getTo());
            }
        }
    }
});

observableList.add("s3");
observableList.set(1, "s1-new-value");

This will result into an output: 这将导致输出:

added: [s3], from: 2, to: 3
added: [s1-new-value], from: 1, to: 2

You can either make use of something like PropertyChangeSuppport or roll your own, for example... 例如,您可以使用诸如PropertyChangeSuppport之类的东西,也可以自己滚动。

public class Person {

    private String name;
    private final PropertyChangeSupport support;

    public Person() {
        this.support = new PropertyChangeSupport(this);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        support.addPropertyChangeListener(listener);
    }

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

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        support.removePropertyChangeListener(listener);
    }

    public void setName(String value) {
        if (name == null ? value != null : !name.equals(value)) {

            String old = name;
            name = value;
            support.firePropertyChange(name, old, name);

        }
    }

    public String getName() {
        return name;
    }                       
}

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

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