Java Reference
In-Depth Information
For the Observer pattern, we use two types: Observable and Observer . 6 The observ-
able entity (the Field in our simulation) extends the Observable class, and the observer
( SimulatorView ) implements the Observer interface (Figure 13.4).
Figure 13.4
Structure of the
Observer pattern
The Observable class provides methods for observers to attach themselves to the observed
entity. It ensures that the observers' update method is called whenever the observed entity (the
field) invokes its inherited notify method. The actual observers (the viewers) can then get a
new, updated state from the field and redisplay.
The Observer pattern can also be used for problems other than a model/view separation. It can
always be applied when the state of one or more objects depends on the state of another object.
13.7.6 Pattern summary
Discussing design patterns and their applications in detail is beyond the scope of this topic.
Here, we have presented only a brief idea of what design patterns are, and we have given an
informal description of some of the more common patterns.
We hope, however, that this discussion serves to show where to go from here. Once we under-
stand how to create good implementations of single classes with well-defined functionality,
we can concentrate on deciding what kinds of classes we should have in our application and
how they should cooperate. Good solutions are not always obvious, so design patterns describe
structures that have proven useful over and over again for solving recurring classes of prob-
lems. They help us in creating good class structures.
The more experienced you get as a software developer, the more time you will spend thinking
about higher-level structure rather than about implementation of single methods.
6 In the Java java.util package, Observer is actually an interface with a single method, update .
Search WWH ::

Custom Search