Observer pattern: case of multiple registration of the same instance

Viewed 183

I'm studying the observer, a kind of design pattern.

The observable object contains a list of observers. A list can accept redundant an observer instance. So we can limit this redundancy using a hash set instead of a list.

My question is, if we allow redundant registration of the same observer instance, is there any example that the observer is registered more than one time? Why the same observer is registered to the same observable multiple times?

1 Answers

One observable may produce several different types of event notifications. One observer may be interested in registering for a number of these notifications. Obviously you could design this scenario in different ways; but one way is to maintain all registrations in a single list.

The GoF does mention on page 296,

It's up to the observer to handle or ignore a notification.

This implies an observer needs to be aware of any potential for redundancy and react appropriately.

Related