Sponsored Link •
The observer pattern
Many of the patterns described in the Gang of Four book show up in the design of the Java API, including the observer pattern. In fact, the observer pattern appears twice in the Java API: once in the
Observer types defined in
java.util, and again as the JavaBeans/1.1 AWT/Swing event
Because the event model described here is used by JavaBeans, the AWT
(1.1 and beyond), and Swing, I will make the claim that this is the
idiomatic way to implement the observer pattern in the Java language.
At the end of this article, I'll explain why I don't feel the
Observable types from
java.util make the grade.
If you are familiar with the Gang of Four book, or any other book of patterns or idioms, you will recognize in the coming text the customary style of presenting patterns. Typically, pattern descriptions all adhere to a common template (that is, they use a common set of text subheadings).
For example, the template I will use for all the idioms I describe in this column has the following form:
The template used to describe patterns varies from book to book, but usually remains the same for all the patterns presented in one book (or in this case, in one column). The template shown above is one I concocted for my Flexible Java project, and I welcome any suggestions you may have as to how it can be improved.