Observer Design Patterns
Observer Design Pattern :-
1.Suppose there's an object which has property of storing weather data which changes continuously .Suppose Weather Department is this object
2.Another object continuously observers/asks that object whether it has changed or not and if it has, it updates according , suppose display on your google weather is this object .And this asking goes on every second or in more frequency , and remember there's are many clients to the weather station so all these clients will be continuously asking for the weather station/observable objects state is changed or not which is kind of unnecessary because that object may not have been changed .
3.To solve this problem , what we do is whenever the observable object changes we push the changed value to the observers and they get notified and updated accordingly. The observers need to be registered for them to get push notified . So , this saves lot of trouble to the observers or clients of weather data.
4.So let's check out the some example interfaces ....
So we have a Iobservable interface which has add() remove() and notify() methods ...the add and remove takes Iobserver's as arguments and adds/removes accordingly to its collection . The notify() method takes no argument
, and what it does is it call update() method of all the Iobservers .
5.Iobserver interface - It has got a update() method which gets called when observable object changes its state ,and that obervable object calls the update method of all the observers with changed value and thus the observers gets updated with the new value .
6.Now all you need is create concrete classes of the above two interfaces and see it in action and also explore code and variation's of this Design Pattern.