这就是所谓的“循环引用”吗?
有了观察者模式,我们都知道,根据其类图,SUBJECT 使用对 OBSERVER 的引用。同时,观察者有一个对主题的引用,以便注册或删除自己。
这是“循环引用”吗?
Having the Observer pattern, we all know that based on its class diagram, the SUBJECT uses a reference to the OBSERVER. Meanwhile, the OBSERVER has a reference to SUBJECT in order to register or remove itself.
Is this a "Circular Reference"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,这是一个循环引用。这是参考图中的一个循环。
不过,当谈论更有害的事情时,有时会使用术语“循环引用”,特别是当您有循环依赖项时(例如,A 需要 B 来编译,但 B 需要 A 来编译)。
循环引用本身并没有什么害处(例如:循环链表)。
Sure, it's a circular reference. It's a cycle in the graph of references.
Using the term "circular references" sometimes occurs when talking about more harmful things though, particularly when you have circular dependencies (e.g. A needs B to compile, but B needs A to compile).
A circular reference in itself isn't harmful (e.g.: a circular linked list).
正如您所描述的,这是循环引用的情况。但是,请注意,在完整模式中,
Observer
是一个抽象类/ 接口,它有一个或多个具体实现。在某些变体中,Observer
对Subject
一无所知(尽管它的子类可能会引用它),在其他变体中,它可能依赖于Observable
接口或直接在主题
上。但是,Subject
只了解Observer
,而不了解其具体子类。因此,引用(不一定)不是循环的。
As you describe it, it is a case of circular reference. However, note that in the full pattern,
Observer
is an abstract class / interface, which has one or more concrete implementations.Observer
in some variations knows nothing aboutSubject
(although its subclasses may refer to it), in other variations it may depend on anObservable
interface or directly onSubject
. However,Subject
knows only aboutObserver
, not its concrete subclasses.So the reference is not (necessarily) circular.