Omar Khaled
Omar Khaled

Reputation: 499

should subject have collection of observers

In the Observer Pattern, should a Subject have a collection of Observers even though I need only one Observer? Every diagram or application I see using Observer Pattern has many Observers. Is it called Observer Pattern if it has only one Observer?

Upvotes: 0

Views: 61

Answers (2)

Mustehssun Iqbal
Mustehssun Iqbal

Reputation: 566

Yes, perfectly okay. Like in MVC, an object in presentation / application facade layer may be the only object that listens to a particular domain object.

Remember the specification of Subject and Observer - there is no mention of a collection there. Observer has an update() method and Subject has a register() method. That's it! Collection is an implementation detail. :)

Upvotes: 0

jaco0646
jaco0646

Reputation: 17066

From the GoF book page 295,

Any number of Observer objects may observe a subject.

This is why you see a collection of Observers in every example. A subject supports more than one; however, it is not required to have more than one. "Any number" includes the number 1.

Upvotes: 2

Related Questions