[英]Java - PropertyChangeListener
I saw an ex. 我看到一个前 using the following for a class: 使用以下内容作为类:
private static PropertyChangeListener listener = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
// Method definition
}
}
1) My first question is that how it could instantiate an interface? 1)我的第一个问题是它如何实例化一个接口?
java.beans: Interface PropertyChangeListener
2) In most examples I've seen, they add PropertyChangeSupport() like following: 2)在我见过的大多数示例中,它们都添加PropertyChangeSupport(),如下所示:
public class MyClass{
private PropertyChangeSupport pcs;
public MyClass(PropertyChangeListener listener) {
pcs = new PropertyChangeSupport(this);
pcs.addPropertyChangeListener(listener);
}
public void method() {
if (condition) {
pcs.firePropertyChange();
}
}
} }
In my class, the method is an static method instead. 在我的课堂上,方法是静态方法。
I did not see in any examples, PropertyChangeSupport defined as static variable and am not sure its right thing to do? 我在任何示例中都没有看到PropertyChangeSupport定义为静态变量,并且不确定要做什么吗?
Does it have any performance issue b/c its going to observe all static variables of the class? 它会观察该类的所有静态变量是否有任何性能问题?
Although firePropertyChange() is fired only for one of the variables. 尽管只对其中一个变量触发firePropertyChange()。
This is how I use it: 这是我的用法:
public class MyClass{
private static PropertyChangeSupport pcs = new PropertyChangeSupport(MyClass.class);
public static void method(PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
if (condition) {
pcs.firePropertyChange();
}
}
}
My class used to be like this, but this method is called many times and I don't want to create a new pcs object every time I call this method, so I changed it like above - define as static variable instead - not sure which practice is better. 我的类曾经是这样的,但是这个方法被调用了很多次,并且我不想每次调用该方法时都创建一个新的pcs对象,因此我像上面那样更改了它-定义为静态变量-不确定哪个练习比较好。
public class MyClass{
public static void method(ConcurrentMap map, PropertyChangeListener listener) {
// pcs is defined to observe only map.
PropertyChangeSupport pcs = new PropertyChangeSupport(map);
pcs.addPropertyChangeListener(listener);
if (condition) {
pcs.firePropertyChange();
}
}
} }
Any help is greatly appreciated. 任何帮助是极大的赞赏。
The first example is possible because you define an anonymus inner class that implemnets the interface. 第一个示例是可能的,因为您定义了一个匿名内部类,该内部类隐含了接口。
For the rest it is not clear what ProperyChangeSupport
is 其余的不清楚是什么ProperyChangeSupport
I would recomend using the ObserverPattern http://en.wikipedia.org/wiki/Observer_pattern 我建议使用ObserverPattern http://en.wikipedia.org/wiki/Observer_pattern
so it would look something like this 所以看起来像这样
public MyClass implements PropertyChangeSupport {
private List<PropertyChangeListener> listener;
public void addPropertyChangeListener(PropertyChangeListener) {
//add the listener
}
public void firePropertyChange() {
//Handle the property change and iterate over all the listener
for(PropertyChangeListener l: listener) {
l.propertyChange(new PropertyChangeEvent());
}
}
public void someMethodThatChangesTheProperty() {
//Do whatever you have to do
this.firePropertyChange();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.