سؤال

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"?

هل كانت مفيدة؟

المحلول

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 about Subject (although its subclasses may refer to it), in other variations it may depend on an Observable interface or directly on Subject. However, Subject knows only about Observer, not its concrete subclasses.

So the reference is not (necessarily) circular.

نصائح أخرى

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).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top