当观察者的寿命比可观察者的寿命长时,何时以及由谁来分离观察者
我在使用公司另一个小组提供的第三方库(用 C++ 编写)时遇到了这个问题。
在 Observer 的析构函数中,它从它订阅的所有可观察对象中分离出来,这部分对我来说很有意义。但在 Observable 的析构函数中,它会检查该 observable 是否有任何仍在其订阅者列表中的观察者。如果是这样,则抛出错误。
我将把它故意在析构函数中抛出错误的事实放在一边。有人可以尝试向我解释为什么可观察者不应该期望观察者能够比它自己活得更长久,或者这只是一个糟糕的设计。如果这是一个糟糕的设计,那么当我们处于观察者比可观察者寿命更长的情况时,有没有好的方法来处理它?
I encountered this problem using a third party library provided by a different group in the company (written in C++).
In the destructor of Observer, it detaches itself from all observables it subscribes to, this part makes sense to me. But in the destructor of Observable, it checks if the observable has any observer that's still in it's subscriber list. If so, throws an error.
I am going to put the fact that it intentionally throws an error in the destructor aside. Can someone try to explain to me the why the observable should expect no observers to outlive itself or is this just a bad design. If this is a bad design, when we are in circumstances when the observer outlives the observable, are there good ways to handle it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果观察者有一个指向 Observable 的指针(或引用),并且 Observable 被销毁,则该指针将无效。作者只是想避免悬而未决的引用。
我认为通常有三种解决方案。
一种是准确执行此代码的操作,也许调用 abort() 而不是在析构函数中抛出异常。
另一种方法是让 Observable 的析构函数从任何观察者中取消注册。
最后一种是使用“智能指针”(例如,引用计数的
shared_ptr
)来保证Observable比任何Observer寿命更长。If the Observer has a pointer (or reference) to the Observable, and the Observable is destroyed, that pointer will be invalid. The author is just trying to avoid dangling references.
There are three usual solutions, I think.
One is to do exactly what this code does, perhaps calling abort() rather than throwing an exception in the destructor.
Another is to have the Observable's destructor de-register itself from any Observers.
The last is to use "smart pointers" (e.g., a reference-counted
shared_ptr
) to guarantee that the Observable outlives any Observer.这取决于上下文。我希望有一个完全通用的
实现观察者模式以支持删除观察者
对象,至少是可选的,但是有很多用途
没有意义,可能是一个编程错误。一般来说,
在 observable 完全销毁之前,它应该通知其观察者:
它会破坏的事实;然后他们应该取消注册。这意味着
当可观察的注册表对象的析构函数是
被叫时,不应有注册观察员。
It depends on the context. I would expect a totally generic
implementation of the observer pattern to support deleting an observed
object, at least optionally, but there are a lot of uses where this
doesn't make sense, and is probably a programming error. In general,
before the observable fully destructs, it should notify its observers of
the fact that it will destruct; they should then unenrol. Which means
that by the time the destructor of the observable registry object is
called, there should be no registered observers.
我通常以相反的方式实现该模式 - Observable 添加自身并将其自身从观察者列表中删除。我的观察者在应用程序的整个生命周期中都存在,而可观察对象通常只存在几秒钟。
I usually implemented the pattern the other way around- the Observable adds itself and takes itself away from the Observer's list. My Observer lives throughout the whole lifetime of the application, and the Observables live a few seconds, typically.
这只是糟糕的设计,没有什么能保证可观察的东西会比所有观察者都活得更久。
如果您有权访问可观察对象的析构函数,请重写它,以便它将分离其剩余的所有观察者。
It's just bad design, nothing guarantees that the observable will outlive all of its observers.
If you have access to the observable's destructor, rewrite it so it will detach all of its observers remaining.