Jface中的属性改变事件观察者模式

2024-01-07 16:18

本文主要是介绍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中的属性改变事件观察者模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/580522

相关文章

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

Java如何通过反射机制获取数据类对象的属性及方法

《Java如何通过反射机制获取数据类对象的属性及方法》文章介绍了如何使用Java反射机制获取类对象的所有属性及其对应的get、set方法,以及如何通过反射机制实现类对象的实例化,感兴趣的朋友跟随小编一... 目录一、通过反射机制获取类对象的所有属性以及相应的get、set方法1.遍历类对象的所有属性2.获取

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

Python中的异步:async 和 await以及操作中的事件循环、回调和异常

《Python中的异步:async和await以及操作中的事件循环、回调和异常》在现代编程中,异步操作在处理I/O密集型任务时,可以显著提高程序的性能和响应速度,Python提供了asyn... 目录引言什么是异步操作?python 中的异步编程基础async 和 await 关键字asyncio 模块理论

禁止平板,iPad长按弹出默认菜单事件

通过监控按下抬起时间差来禁止弹出事件,把以下代码写在要禁止的页面的页面加载事件里面即可     var date;document.addEventListener('touchstart', event => {date = new Date().getTime();});document.addEventListener('touchend', event => {if (new

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

滚雪球学Java(87):Java事务处理:JDBC的ACID属性与实战技巧!真有两下子!

咦咦咦,各位小可爱,我是你们的好伙伴——bug菌,今天又来给大家普及Java SE啦,别躲起来啊,听我讲干货还不快点赞,赞多了我就有动力讲得更嗨啦!所以呀,养成先点赞后阅读的好习惯,别被干货淹没了哦~ 🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!! 环境说明:Windows 10

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法