1

In a classic implementation of the Observer design pattern, the subject has a list of references to all of it's observers, and each observer might have a reference to it's subject.

Is this considered composition? As far as I understand, composition means one object 'includes' in itself another object, in order to use functionality of that object. In the Observer pattern, the subject does include in itself references to it's observers, but it doesn't exactly use their functionality internally to do things outwardly. It keeps references so it can notify them.

On the other hand, the subject and observers maintain a relationship of HAS-A, which often characterizes composition (the subject has observers, the observers have a subject).

Is this considered a kind of composition?

  • 1
    typically observer and observable are unrelated except that the observer want to get notified, each observer may have multiple objects it is observing and vice versa – ratchet freak Mar 19 '14 at 9:32
  • It is not always necessary for the observer to keep reference of the subject. Subject can pass itself during the notification. – Saran Mar 20 '14 at 9:27
1

Being notified as per the observer pattern is part of the functionality of the observing object. Insofar, this is an instance of composition.

It's just not a very large part of its functionality: typically, it means implementing one simple method to satisfy one very light-weight interface. Therefore, this is in no way a prototypical example for what people will think of when you mention "composition" to them, the coupling is much looser than it might be, etc. All this teaches us is that even seemingly clear-cut definitions are almost always a matter of degree in practice.

1

The observer pattern as it is shown in Wikipedia seems to be indeed based on composition - for calling the notify() method, the subjects needs to "own" the observers. The criteria for this not the usage of an object, but the lifetime - when the lifetime of the subject ends, the lifetime of the observers (which are parts of the subjects) also ends.

However, the original Observer Pattern from the GOF book is better known under the name publisher-subscriber: the notification of other objects works here in a similar manner, but the lifetime of the "observers" and the "observed" objects is completely independent. Thus, publisher-subscriber needs a little bit more effort to implement, subscriber objects (=observers) have to register themselves at the publisher objects (=subject), or to deregister when they don't want to be notified any more. And when the subject's life time ends, the observer objects can still remain, they just won't get any more notification.

So the answer is: it depends which "observer" pattern definition you are using.

  • we must have different GOF books, since the Observer is publish-subscribe in the one on my table... (plus it's not an aggregation, but just a reference/association). – Lovis Mar 19 '14 at 14:17
  • 1
    @DonL.: you are right, see my edited answer. – Doc Brown Mar 19 '14 at 14:28
0

In and of itself the observer pattern is not based on composition - it does not require composition.

Your object "has" an observer now, but the next second its observer can be gone (disattached).

Contrary to what composition implies, the lifetimes of the observer and the observable can be independent of eachother, so it's not really a "has-a" relationship. I believe it's an example of aggregation.

If your implementation is tightly coupled and the observable instantiates its own observers, then they'll not survive after the observable is disposed and thus this is indeed composition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.