can an observer observe multiple observables?

前端 未结 5 1705
南笙
南笙 2021-02-10 16:14

trying to find an example of this, it\'s possible that I am not going the right way around it, or that my mind has over simplified the concept of the observer pattern.

I

5条回答
  •  有刺的猬
    2021-02-10 17:03

    From GoF (implementation section of the Observer pattern), "Observing more than one subject. It might make sense in some situations for an observer to depend on more than one subject. For example, a spreadsheet may depend on more than one data source. It's necessary to extend the Update interface in such cases to let the observer know which subject is sending the notification. The subject can simply pass itself as a parameter in the Update operation, thereby letting the observer know which subject to examine."

    The answer from Mchl already contains the example. I am just adding the reference from GoF that it will not be a bad practice if you need to do this.

提交回复
热议问题