Search code examples
phpobserver-pattern

Who is the observer and who is the observable?


In a large modular application, how do you determine who is the observer and who is the observable?

For example, let's say we have 2 modules:

  • Blog module: Create and manage blog posts.
  • Social media module: read and write to social media streams.

Assuming that we want to implement a feature where when a user is on the "create a blog" post page, if he has the social media module enabled, we also include a set of fields which deals with posting to social streams.

In this case, should the blog module or the social media module be the observer? Who should provide the form fields for the social media options? Should the social media module provide a full widget or should the blog module detect whether the social media module is avaliable, render appropriate fields and then pass the submitted data to the social media module?


Solution

  • The observer is the module that wants to be updated when data from the other module changes. In the example you provided, the social medial module is the observer and the blog module is being observed. Each social media module (observer) should register with the blog passing a handle or reference the would be called whenever the blog updates.