简体   繁体   English

观察者/可观察模式的链接

[英]Chaining of observer/observable pattern

Is there a design pattern that forms a "composite" observer/observable? 是否存在形成“复合”观察者/可观察者的设计模式?
I mean I have an observable A that notifies its listeners on some change. 我的意思是我有一个可观察的A ,它会通知听众一些变化。
Each listener is also an observable and notifies its own listener (on some action it did which action was triggered by notification of the first observable). 每个监听器也是一个可观察的,并通知其自己的监听器(在某些动作上,它通过第一个可观察的通知触发了哪个动作)。
Is this "chaining" of observers/observables ok as design or is there a standard pattern for this? 观察者/观察者的这种“链接”是否可以作为设计或是否有标准模式?

For the chaining you mentioned, I don't see any difference. 对于你提到的链接,我看不出任何区别。

The aim of Observer pattern is, when the state of an object is changed, it notify interested parties (listener/observer) about the change and let the listener react according to the state change. Observer模式的目的是,当对象的状态发生变化时,它会通知感兴趣的各方(监听者/观察者)有关变化的信息,并让监听者根据状态变化做出反应。

If the state change of the listener is, by design, observed by other interested parties, I don't see any reason why I can't use observer pattern for the publishing the event. 如果听众的状态变化在设计上被其他感兴趣的人观察到,我认为没有任何理由不能使用观察者模式来发布事件。

However it is simply another observer-observable relationship. 然而,它只是另一种观察者可观察的关系。 There is no special "chaining" happening. 没有特殊的“链接”发生。

A bit OT: something that is more appropriate to be called chaining is: An observed object O send an event E to listener A, and A will propagate the event to another listener B. However logically they are all listening to event originated from O. Such kind of "chaining" you can be done by using decorator pattern. 有点OT:更适合称为链接的东西是:观察对象O向侦听器A发送事件E,A将事件传播到另一个侦听器B.但从逻辑上讲,它们都在监听源自O的事件。这种“链接”你可以通过使用装饰模式来完成。

Your classes should implement both interfaces: observer and subject. 您的类应该实现两个接口:observer和subject。 Challange is registering all observers to proper objects. Challange正在将所有观察者注册到适当的对象。
All you need is to add notifyObservers() in notify() method in Observer classes ( link to wiki ). 您只需要在Observer类的notify()方法中添加notifyObservers()链接到wiki )。
Note you can start infinite loop of notifications: A => B => C => A => ... and so on. 请注意,您可以启动无限循环通知: A => B => C => A => ...依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM