如何设计具有不同类型的通知和观察者的观察者模式? [英] How do I design an Observer pattern with different types of notifications and observers?

查看:48
本文介绍了如何设计具有不同类型的通知和观察者的观察者模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:假设我有一个基类 SketchbookEventObserver 和派生类 MouseClickObserver TouchObserver TextChangedObserver ./p>

所有这些 SketchbookEventObserver 都必须向网络请求有关发生的事件的数据:

MouseClickObserver -鼠标单击的坐标.

TouchObserver -触摸的坐标和持续时间.

TextChangedObserver -具有文本框标识符的旧文本和新文本.

所有这些观察者都注册在 UIEventRegistry 类上.事件发生时,它将在每个观察者上调用 OnEvent ,并作为参数传递:

  1. 事件类型-鼠标单击/触摸/文本已更改.这由ID表示.
  2. 有关事件的数据-如上所述.每种类型的事件将具有不同类型的数据.

但是,我无法在每个派生类中使用不同的输入参数覆盖 OnEvent .如果我具有通用和多态的输入参数,例如,使用 GetData()函数说出 EventData ,我仍然需要覆盖其中的 GetData() EventData 的派生类,它们将具有不同的返回值.这也是不可能的.

另一种选择是在这些观察者之间不具有任何继承,并将它们视为单独的实体. EventRegistry 将具有每个类型的观察者的数组/列表,其中的类型是已知的,然后调用 mMouseClickObservers [i] .OnEvent()进行鼠标单击事件,<触摸事件的代码> mTouchObservers [i] .OnEvent(),依此类推.

但这意味着 EventRegistry 将需要具有有关具体类的知识,如果 EventRegistry 是另一个库/包的一部分,则需要公开公开这些具体类.

有没有更好的方法来设计这个?

解决方案

另一种选择是在这些观察者之间没有任何继承,并将它们视为单独的实体.

这是我最喜欢的选项,也是我多次提倡的选项,因为它可以维护事件的类型安全性.如果每个事件都不相同,请不要通过相同的API来处理所有事件.

但这意味着 EventRegistry 将需要具有有关具体类的知识...

MouseClickObserver TouchObserver TextChangedObserver 应该都是抽象的.现在,每个抽象可能只有一个实现.但是设计不应该强制这样做.

Scenario: say I have a base class SketchbookEventObserver and derived classes MouseClickObserver, TouchObserver and TextChangedObserver.

All of these SketchbookEventObservers have to make a network request with data about the event that happened:

MouseClickObserver - coordinates of the mouse click.

TouchObserver - coordinates and duration of touch.

TextChangedObserver - the old and new text with a text box identifier.

All of these observers are registered on a UIEventRegistry class. When an event happens, it woud call OnEvent on each observer, and pass as params:

  1. The type of event - mouse click / touch / text changed. This is represented by an ID.
  2. The data about the event - as described above. Each type of event will have different types of data.

However, I cannot override OnEvent with different input parameters in each of the derived classes. If I have the input parameter generic and polymorphic, say EventData with a GetData() function, I would still need to override GetData() in derived classes of EventData, which will have different return values. This is also impossible.

The other option is to not have any inheritance between these observers, and treat them as separate entities. EventRegistry will have an array / list of observers of each type, where their types are known, then call mMouseClickObservers[i].OnEvent() for a mouse click event, mTouchObservers[i].OnEvent() for a touch event, and so on.

But this means that EventRegistry will need to have knowledge about the concrete classes, which need to be exposed publicly if EventRegistry is part of a different library / package.

Is there a better way to design this?

解决方案

The other option is to not have any inheritance between these observers, and treat them as separate entities.

This is my favorite option, and one that I've advocated many times because it maintains type safety of the events. If each event is different, don't handle them all through the same API.

But this means that EventRegistry will need to have knowledge about the concrete classes...

MouseClickObserver, TouchObserver, and TextChangedObserver should all be abstractions. You may only have one implementation of each abstraction now; but the design should not enforce that.

这篇关于如何设计具有不同类型的通知和观察者的观察者模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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