15.2. Observable and Observer Objects

The Observable class provides you with an interesting mechanism for communicating a change in one class object to a number of other class objects. One use for this mechanism is in graphical user interface (GUI) programming where you often have one object representing all the data for the application—a text document, for example, or a geometric model of a physical object—and several other objects that represent views of the data that are displayed in separate windows, where each shows a different representation or perhaps a subset of the data. This is referred to as the document/view architecture for an application, or sometimes the model/view architecture. This is a contraction of something referred to as the model/view/controller architecture, and we will come back to this when we discuss creating GUIs. The document/view terminology is applied to any collection of application data—geometry, bitmaps, or whatever. It isn't restricted to what is normally understood by the term document. Figure 15-2 illustrates the document/view architecture.

Figure 15.2. Figure 15-2

When the Document object changes, all the views need to be notified that a change has occurred, since they may well need to update what they display. The document is observable, and all the views are observers. This is exactly what the Observable class is designed to achieve when ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.