本文主要是介绍Jface中的属性改变事件观察者模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Jface中的属性改变事件观察者模式
1.模式涉及的类
org.eclipse.jface.util.IPropertyChangeListener; (接口)
org.eclipse.jface.util.PropertyChangeEvent;(类)
2.类图
3.使用方式
3.1.定义一个管理器
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;public class ValuePropertChangeManager {private static ValuePropertChangeManager INSTANCE;private static final Map<String, IPropertyChangeListener> map = new HashMap<String, IPropertyChangeListener>();private ValuePropertChangeManager(){}public static ValuePropertChangeManager getInstance() {if(INSTANCE==null)INSTANCE = new ValuePropertChangeManager();return INSTANCE;}// 触发属性改变监听事件public void firePropertyChangeListener(PropertyChangeEvent event) {final Iterator iter = map.keySet().iterator();while (iter.hasNext()) {map.get(iter.next()).propertyChange(event);}}// 注册属性改变监听器public void addPropertyChangeListener(final String listenerID,final IPropertyChangeListener listener) {if (!map.containsKey(listenerID))map.put(listenerID, listener);}// 注销属性改变监听器public void removePropertyChangeListener(final String listenerID) {if (map.containsKey(listenerID))map.remove(listenerID);}
}
3.2.定义事件生产者A
import org.eclipse.jface.util.PropertyChangeEvent;public class MyPropertyProviderA {private String myValue; public void setValue(String curValue){String oldValue = myValue;myValue = curValue;ValuePropertChangeManager.getInstance().firePropertyChangeListener(new PropertyChangeEvent(this, "myValue", oldValue, myValue));}
}
3.3.定义事件生产者B
import org.eclipse.jface.util.PropertyChangeEvent;public class MyPropertyProviderB {private String myValue; public void setValue(String curValue){String oldValue = myValue;myValue = curValue;ValuePropertChangeManager.getInstance().firePropertyChangeListener(new PropertyChangeEvent(this, "myValue", oldValue, myValue));}
}
3.4.定义事件消费者A
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;public class MyPropertyOberverA implements IPropertyChangeListener{public void registerMyValueChange(){ValuePropertChangeManager.getInstance().addPropertyChangeListener("MyPropertyOberverA", this);}@Overridepublic void propertyChange(PropertyChangeEvent event) {// TODO Auto-generated method stubSystem.out.println("-----MyPropertyOberverA do for propertyChange-----");System.out.println("source class="+event.getSource().getClass());System.out.println("Property="+event.getProperty());System.out.println("old value="+event.getOldValue());System.out.println("new value="+event.getNewValue());}
}
3.5.定义事件消费者B
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;public class MyPropertyOberverB implements IPropertyChangeListener{public void registerMyValueChange(){ValuePropertChangeManager.getInstance().addPropertyChangeListener("MyPropertyOberverB", this);}@Overridepublic void propertyChange(PropertyChangeEvent event) {// TODO Auto-generated method stubSystem.out.println("-----MyPropertyOberverB do for propertyChange-----");System.out.println("source class="+event.getSource().getClass());System.out.println("Property="+event.getProperty());System.out.println("old value="+event.getOldValue());System.out.println("new value="+event.getNewValue());}
}
3.6.测试例程
public class Test {public static void main(String[] args) {// TODO Auto-generated method stubMyPropertyOberverA poa = new MyPropertyOberverA();poa.registerMyValueChange();MyPropertyOberverB pob = new MyPropertyOberverB();pob.registerMyValueChange();MyPropertyProviderA ppa = new MyPropertyProviderA();ppa.setValue("value 1");MyPropertyProviderB ppb = new MyPropertyProviderB();ppb.setValue("value 0.618");}}
3.7.运行结果
-----MyPropertyOberverB do for propertyChange-----
source class=class com.tyde.jfcs.ui.test.MyPropertyProviderA
Property=myValue
old value=null
new value=value 1
-----MyPropertyOberverA do for propertyChange-----
source class=class com.tyde.jfcs.ui.test.MyPropertyProviderA
Property=myValue
old value=null
new value=value 1
-----MyPropertyOberverB do for propertyChange-----
source class=class com.tyde.jfcs.ui.test.MyPropertyProviderB
Property=myValue
old value=null
new value=value 0.618
-----MyPropertyOberverA do for propertyChange-----
source class=class com.tyde.jfcs.ui.test.MyPropertyProviderB
Property=myValue
old value=null
new value=value 0.618
4.说明
相对jdk的观察者模式的Subject和Observer方式,
(1)支持多事件触发者;
(2)支持无法或者不方便在同一处设置观察者和事件产生者关联关系的情况;
这篇关于Jface中的属性改变事件观察者模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!