I want to use the Java Observer pattern in my project. With that in mind, can you give me some sample code to demonstrate
how it works?
Just as object-oriented programming encourages code reuse, design patterns encourage design reuse. Indeed, design patterns
allow you to reuse clean, time-tested designs. However, design patterns have come under an increasing amount of criticism
lately. Critics point out that inexperienced developers can easily fall into the pattern trap.
The pattern trap blinds inexperienced developers. As such, instead of solving a solution in the best way possible, the end goal for novice developers centers on implementation of as many design patterns as possible. To some, using a design pattern seems to guarantee a good design. By that logic, if you use a lot of design patterns, you'll have a great design! Often times this belief leads to designs that just don't make sense -- even though the design might incorporate multiple patterns. Unfortunately, a design pattern does not guarantee good design.
In order to properly use a design pattern in your design, you must uphold three criteria:
You need to know criterion number 1 first and foremost. How can you apply a pattern if you do not fully know what problem you are trying to solve?
You also need to know the second criterion: You must fully understand the patterns you are interested in applying. How can you apply a pattern if you do not understand it? More importantly, how can you even consider a pattern if you do not understand what it does?
Finally, if you cannot articulate how the pattern will solve your problem (why it is appropriate), forget it. Don't fall into the pattern trap and simply use a pattern so you can say you used it.
I'm not saying that the reader is necessarily falling into this trap. However, by the wording of the question, many developers may get the wrong impression about patterns. From the question, I understand that the reader probably knows the problem and understands the Observer pattern. He or she simply needs to see the pattern implemented in Java.
Before giving a Java example, it may help other readers to first give a brief description of the Observer pattern.
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.
That is what I neededBy Anonymous on January 25, 2010, 6:22 pmI saw one using this Design Pattern. Even though I'm not a pro in programming, I thought of understanding this concept and the only place where it got cleared at...
Reply | Read entire comment
Thanks, a very simple and effective way of explaining this patteBy Anonymous on January 23, 2010, 5:07 pmThanks, a very simple and effective way of explaining this pattern. An example speaks a thousand words!
Reply | Read entire comment
fantastic By Anonymous on January 11, 2010, 6:02 amfantastic
Reply | Read entire comment
ThanksBy Anonymous on December 30, 2009, 10:20 amGood One
Reply | Read entire comment
coolBy Anonymous on October 30, 2009, 3:43 pmit is a good example tnx
Reply | Read entire comment
View all comments