The Observer pattern explained

The intent of the Observer pattern is to define a one-to-many relationship between objects. When the state of one object changes, all its dependents are notified. The typical names for the objects in this pattern are the Subject (the one), and the Observers (the many). The Subject will contain data that the Observers need to know about. Instead of the usual situation of classes requesting data from another (polling), our Subject will notify a list of Observers when the data has changed.

The terms Subject and Observers may seem a little confusing at first. However, this concept is very easy, and one that most of us are familiar with. When trying to understand the Observer pattern, think of a blog and subscribers. In this case, the blog is the Subject and the subscribers are the Observers.

A blog may be updated once a day, once a week, once a month, or even less. Readers of the blog have the option to check for updates as much as they want, however this can waste a lot of time if the readers check more frequently than the blog is updated. Instead, fans will often choose to subscribe to an e-mail list so they can be notified of updates as they come. The blog keeps a list of subscribers and sends an e-mail to everyone on the list when an update is posted.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.31.165