Campbell Ritchie wrote:You can instantiate an interface by supplying a class body and all the methods of that interface. I haven't enough time to explain fully now, but if you search this website for "anonymous class" you will probably find something useful.
Campbell Ritchie wrote:You can instantiate an interface by supplying a class body and all the methods of that interface.
pete stein wrote:It allows a class to respond to events in other classes without coupling them too much -- without the two classes having to know intimate details about the other.
Seetamraj Sriharsha wrote:So what does Frame class gain when it listens to the event of Panel ? That is what I understood
pete stein wrote:If you didn't have a listener, and you wanted the panel to tell the JFrame to display an optionpane when a button has been pressed, the panel will have to hold a reference to the frame class somewhere and then in its code call a method of the frame class when the button has been pressed. So now you have the frame holding a reference to the panel and a panel holding a reference to the frame which == tight coupling, which is a bad thing and can make it hard to debug or extend your program.
Simply, the Observer pattern allows one object (the observer) to watch another (the subject). The Observer pattern allows the subject and observer to form a publish-subscribe relationship. Through the Observer pattern, observers can register to receive events from the subject. When the subject needs to inform its observers of an event, it simply sends the event to each observer.
For example, you might have a spreadsheet that has an underlying data model. Whenever the data model changes, the spreadsheet will need to update the spreadsheet screen and an embedded graph. In this example, the subject is the data model and the observers are the screen and graph. When the observers receive notification that the model has changes, they can update themselves.
See where your hand is? Not there. It's next to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|