观察者-明确指定感兴趣的修改-JAVA实现 [英] Observer - specifying modifications of interest explicitly - JAVA implementation

查看:48
本文介绍了观察者-明确指定感兴趣的修改-JAVA实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了此处当我需要通知<仅当 对象 的特定属性发生变化时,em> 观察者 .

I asked here how I should handle the situation when I need to inform observers only when specific attribute of object changes.

我收到关于 GoF 的非常好的回复,写在:

I received very nice reply with reference to GoF where is written:

Blockquote明确指定感兴趣的修改.您可以通过扩展主题的注册界面以仅针对感兴趣的特定事件注册观察者来提高更新效率.当发生此类事件时,主题仅通知那些对该事件具有浓厚兴趣的观察者.一种支持此方法的方法是使用Subject对象的方面概念.为了引起对特定事件的兴趣,观察员使用

Blockquote Specifying modifications of interest explicitly. You can improve update efficiency by extending the subject's registration interface to allow registering observers only for specific events of interest. When such an event occurs, the subject informs only those observers that have registered interest in that event. One way to support this uses the notion of aspects for Subject objects. To register interest in particular events, observers are attached to their subjects using

void主题::附加(观察者*,关注方面);

Blockquote其中,兴趣指定事件.在通知时,主题会将更改的方面作为更新操作的参数提供给其观察者.例如:

Blockquote where interest specifies the event of interest. At notification time, the subject supplies the changed aspect to its observers as a parameter to the Update operation. For example:

void Observer :: Update(Subject *,Aspect& interest);

这很有意义,我想问一下如何在Java中正确实现它.我的想法不多,但我不确定是否还有更好的方法.

This makes sense and I would like to ask how to correctly implement this in Java. I have few ideas but I am not sure if there isn't something better.

假设我有 主题 WeatherStation [温度,湿度,风速...] ,而我有 观察者 LimitedDisplay [show(仅显示温度和湿度),出于某种原因,我需要在仅更改温度和仅更改湿度时,显示器应该能够区分已更改.

Let's imagine I have subject class WeatherStation[temperature, humidity, windSpeed ...] and I have observer class LimitedDisplay[show (shows only temperature and humidity) and for some reason I need that the display should be able to differentiate when only temperature was changed and when only humidity was changed.

我当时以为我可以创建一些枚举 WeatherStationInterest [TEMPERATURE,HUMIDITY WIND_SPEED ...] ,然后具有如下主题界面:

I was thinking that I could create some enum WeatherStationInterest[TEMPERATURE, HUMIDITY WIND_SPEED...] and then have the subject interface like this:

public interface WeatherStationSubject{
    registerObserver(WeatherStationObserver observer, WeatherStationInterest... interests);
    .
    .
    .
}

有人可以向我确认这是正确的方法还是建议我更好的解决方案.

Could someone confirm me that this is correct way how to do it or advice me better solution.

推荐答案

另一种选择是拥有一组接口:

Another option is to have a set of interfaces:

interface TemperatureListener {
    onTemperatureChange(double newTemperature);
}
interface HumidityListener {
    onHumidityChange(double newHumidity);
}
...

然后注册方法如下:

void registerListener(Object listener)

您将实现一个侦听器:

class MyListener implements TemperatureListener, HumidityListener {
...
}

不一定更好,但是如果重要的话,可以轻松查看实际更改.

Not necessarily better, but makes it simple to see what has actually changed, if that's important.

这篇关于观察者-明确指定感兴趣的修改-JAVA实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆