samedi 25 juillet 2015

ObserverPattern - Web implementation example (Twitter)

ObserverPattern
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

I understand the definition, and I understand it from the windows forms perspective. For example we have three forms and we can update three forms at the same time (different observers) while subjects state changes.

But if we want to implement observer pattern on website(example Twitter), how does it work?

Let's say we have a subject Bill Gates, and 20 followers join him(20 new observers).

Each time new follower joins, isn't this stored in database who is which follower? So in case Bill Gates tweets something, operation checks in database who is his follower and updates wall of followers with Bill Gates tweet(which goes again into database under followers ID).

So how is Twitter using observer pattern (Subject-Observers) from the design pattern perspective? Do we still use observer pattern, and while someone new follows, we use method registerObserver to insert observer in database of observers for Bill? And then when Bill tweets something new, it loops through all observers of Bill from database and notifies them?

I am trying to understand this pattern in real life code usage on Twitter example.

I checked this video about Observer Pattern: https://www.youtube.com/watch?v=YIX9Bkl3ZPE

I understand when we are implementing directly in WinForms(I am talking about usage). But what happens when database comes in? For example for web. Is it the same, and when is the best to use it, I would need one web example.

Aucun commentaire:

Enregistrer un commentaire