观察者模式:独立观察属性

问题描述:

我想问当我需要实现这样的目标时应该如何正确实现观察者模式:

I would like to ask how I should correctly implement observer pattern when I need to achieve something like this:

WeatherStation[temperature, humidity ...]

,我需要能够观察"每个属性独立.因此,当温度变化时,只会通知温度观测者,而当湿度变化时,只会通知湿度用户.

and I need to be able to "observe" each attribute independently. So when temperature changes only temperature observers will be notified, when humidity changes only humidity subscribers will be notified.

我的想法是,我将创建一些类,例如ObservableTemperature和接口TemperatureObserver,但是通过这种方式,我将不得不创建两个"classes"类.每个属性.

My idea was that I would create some classes like ObservableTemperature and interface TemperatureObserver but I this way I would have to create two "classes" for each attribute.

第二个选项是为每个属性仅创建两个接口(类似TemperatureSource,TemperatureObserver等),然后在WeatherStation类中实现xxxSource接口,但这种方式不可重用,我需要在WeatherStation类中具有跟踪观察者的大量数组(与可观察的"属性相同的数量).

Second option is to create only two interfaces for each attribute (something like TemperatureSource, TemperatureObserver ...) and then implement the xxxSource interface in WeatherStation class but this way it is not reusable and I would need to have in WeatherStation class a lot of arrays (same number as "observable" attributes) keeping track of observers.

还有更好的选择吗?

同样,也可能发生类似Display类的事件,该事件将订阅多个属性(不是全部),并且仍然需要区分其中哪一个已更新.

EDITED: Also it can also happen that I would have something like Display class which would subscribe to multiple attributes(not all) and still need to distinguish which one of them was updated.

温度湿度等组合到一个类中 WeatherStation 定义一个领域的概念.就观察者模式而言,这是一门学科.另一方面,发送由单个值组成的通知会将 WeatherStation 划分为多个域概念和多个主题.显然,这两个设计决策之间存在冲突.

Combining temperature, humidity, etc. into a class WeatherStation defines one domain concept. In terms of the Observer pattern, this is one subject. On the other hand, sending notifications consisting of single values would divide WeatherStation into multiple domain concepts and multiple subjects. Clearly there is a conflict between these two design decisions.

GoF模式是根据对象(而非字段)定义为主题的.但是请注意,这并不限制主题在不同时间通知不同的观察者.本书的相关部分从第298页开始.

The GoF pattern is defined in terms of objects (not fields) as subjects. But note this does not restrict a subject from notifying different observers at different times. The pertinent section of the book begins on page 298.

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

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 Subject::Attach(Observer*, Aspects interest);

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

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);

这种方法使不同的观察者可以从一个 Subject 注册不同的通知.请注意,无论观察者注册于哪个方面,它都会在通知消息中收到相同的 Subject .观察者可以从 Subject 读取必需的字段.

This approach enables different observers to register for different notifications from one Subject. Note that regardless of which aspect(s) an observer registers for, it receives the same Subject in the notification message. It is up to the observer to read the necessary field(s) from the Subject.