Search code examples
phpdesign-patternsobserver-pattern

when to use the observer pattern when developing websites?


i need some practical examples of cases when i could use the observer pattern when developing a website.. (using php)

I have one "when a user publishes an Article (subject), the class RSS and the class EMAIL (the observers) will modify the rss and send an email to the admin", but i'm not even sure if this is a good example..

where are you using the observer pattern?

BTW: this is not a homework, i was just lying here thinking about this pattern :)

EDITED I'm more curious about the "WHEN to do it" and not the "HOW to do it"


Solution

  • I have a collection (array) of objects (cells) as the property of a "cellCollection" object. To reduce memory usage, each cell is actually held in serialized form in a cache (disk file, APC, memcache, whatever) while the "cellCollection" object holds its array of pointers to the cache location. I use the observer pattern so that the "cellCollection" object is notified whenever a "cell" object is modified, so that it can update the master copy of that "cell" object in cache and adjust its pointers as necessary.