mardi 22 mai 2018

Events vs Observer pattern

I found some resources on this, but still can't get my head around it (also, didn't found question asking this directly).

Let's suppose: we have one object of interest and multiple objects interested in this object (its state for example). When object of interest changes in some way, others want to know about it.

First approach

It looks like classic problem that is solved by Observer Pattern. When object of interest changes it invokes methods on objects that are subscribing the event. Simple.

Second approach

Define an event in object of interest, on change, raise this event. Others will listen to this event and call their methods on it.

One obvious difference is that the object invoking method is different. But, why we should use one instead of the other approach?

I know these concepts, but I am looking for deep understanding of a problem.

Thanks for any hints!

Aucun commentaire:

Enregistrer un commentaire