
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
A Generalized MVC Implementation
|
393
implementation. The model class in our MVC architecture will be a subclass of
Observable, which resides in the package util. The Observer pattern provides the
basic services for the model-view relationship—views register with the model using
addObserver( ) and removeObserver( ), and the model sends updates via the stan-
dardized method update( ), implemented by each view. Hence, in order to register
with a model, every view class must implement the util.Observer interface in addi-
tion to implementing the mvc.View interface.
By definition, to use the Observable class as the basis of our model class, the model
class must extend Observable. If we want our model class to inherit from a different
class, we’d have to use the composition-based implementation of the Observer pat-
tern, from Example 16-7.
The View Implementation
Example 18-1 shows the View interface in our mvc package. The View interface
defines the basic services every View class must offer, namely:
• Methods to set and retrieve the controller reference
• Methods to set and retrieve the model reference
• A method that returns the default controller for the view
Notice that references to the controller in Example 18-1 must be instances of a class
that implements the Controller interface. References ...