观察者可以观察到多个可观察对象吗?
试图找到一个这样的例子,有可能我没有采取正确的方法,或者我的思想过度简化了观察者模式的概念。
我想创建一个类来控制来自 Web 服务的消息,并且我希望该类监视许多其他操作的更改。
我见过的观察者模式示例表明许多观察者正在观看单个可观察的内容,我可以(或者应该)反过来做吗?我还应该做什么?
trying to find an example of this, it's possible that I am not going the right way around it, or that my mind has over simplified the concept of the observer pattern.
I want to create a class that controls messages from a web service, and I want this class to monitor the changes of many other operations.
The observer pattern examples I have seen demonstrate many observers watching a single observable, can I (or should I) do this the other way round? What else should I be doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需在许多 Oservable 实例中注册一个 Observer 实例即可。
每当 Observable 更新时,您可能希望将 Observable 实例传递给 Observer,以便 Observable 知道哪个特定对象更新了它。
一个简单的例子:
您可能还想查找中介模式。
这是它的非常好的实现: Symfony Event Dispatcher
Just register a single Observer instance in many Oservable instances.
You probably will want to pass Observable instance to Observer, whenever Observable updates, so that Observer knows which specific object updated it.
A trivial example:
Also you might want to lookup the Mediator pattern.
Here's pretty nice implementation of it: Symfony Event Dispatcher
我认为记住设计模式只是建议而不是绝对非常重要。如果可以对它们进行修改以更好地满足您的需求,那么它们就应该进行修改。
是的,在这种情况下,这是绝对可以做到的。您的观察者只需要注册多个可观察对象即可。
当你的一个可观察对象通知它的观察者时,它只会循环引用列表来告诉他们更新。这些引用是否与其他可观察对象共享并不重要。
I think it's really important to remember that design patterns are suggestions and not absolutes. If they can be modified to better fit your needs then they should.
And yes in this case it can most definitely be done. Your observers will simply need to register with more than one observable object.
When one of your observable objects will notify it's observers it will simply loop on a list of references to tell them to update. It really doesn't matter if these references are shared with other observable objects.
来自 GoF(观察者模式的实现部分),“观察多个主题。在某些情况下,观察者依赖多个主题可能是有意义的。例如,电子表格可能依赖于在这种情况下,有必要扩展 Update 接口,让观察者知道哪个主体正在发送通知。主体可以简单地将自身作为 Update 操作中的参数传递,从而让观察者知道哪个主体。检查。”
Mchl 的答案已经包含了这个例子。我只是添加 GoF 的参考资料,如果您需要这样做,这将是一个不错的做法。
From GoF (implementation section of the Observer pattern), "Observing more than one subject. It might make sense in some situations for an observer to depend on more than one subject. For example, a spreadsheet may depend on more than one data source. It's necessary to extend the Update interface in such cases to let the observer know which subject is sending the notification. The subject can simply pass itself as a parameter in the Update operation, thereby letting the observer know which subject to examine."
The answer from Mchl already contains the example. I am just adding the reference from GoF that it will not be a bad practice if you need to do this.
我同意 1 个类执行 1 个核心任务的想法。如果是我,我会构建一个观察者接口,创建多个观察者实现,并使用某个 ObserverManager 类管理所有这些观察者。
这样做将分离您所有的业务问题,并提供更精细的测试粒度。
除非“许多其他操作的变化”可以被典型为同一种可观察的“变化”。到那时,单一观察者就有意义了。
I subscribe to the idea that 1 class performs 1 core task. If it were me, I would build an observer interface, create multiple observer implementations, and manage all of those observers with some ObserverManager class.
Doing it this way will separate all of your business concerns, and will afford a finer level of granularity for testing.
Unless the "changes of many other operations" can be typified as the same kind of observable "change." At that point the the single observer makes sense.