Well to be frank about his; I’m a newbie who doesn’t know about Design Patterns. I just came across this today while I was coding and one of my associates helped me with this pattern.
This example might be simple and even idiotic for those pro’s who are swimming in the Java world. This is for those beginners who need to know the spark in this concept ;)
One might think differently after going through this…. trust me… even if you don’t understand :P
Well I found info about this pattern over here, but I just took conceptual part from it.
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.
This is what it had to say… ;)
Just another way,
Suppose you wanted the Panel to tell the frame to display a message box 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.
I think you got it…. If not comment it down…. :D
So, Let’s talk about business…
I will post regarding Anonymous classes ASAP…
This is a simple application which changes text of button in another panel. Text on button in second panel changes if an event occurs in first panel and vice versa.
Following is the code which explains this….