Reputation: 499
In the Observer Pattern, should a Subject
have a collection of Observer
s even though I need only one Observer
? Every diagram or application I see using Observer Pattern has many Observer
s. Is it called Observer Pattern if it has only one Observer
?
Upvotes: 0
Views: 61
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
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