
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
392
|
Chapter 18: The Model-View-Controller Design Pattern
What Creates the MVC Classes?
Through all this talk of the model, view, and controller, we still haven’t seen how to
make instances of those classes. That’s partly because there’s no single, definitive
way to instantiate the classes in MVC. In our clock example, we’ll create a single
class, Clock, which instantiates the model, its views, and their controllers. Our Clock
class sets up the MVC classes as follows:
• Create the model
• Create views
• Register views with the model
Notice that the controllers are missing from the preceding list because they are cre-
ated by their respective views.
Hence, our Clock class forms a wrapper around the MVC triad, packaging it into a
tidy, self-contained unit. However, that’s definitely not the only approach possible.
At least one Java implementation suggests that the controller class should create the
model, and possibly the view, in its constructor!
In our example code, we’ll follow the traditional (Smalltalk) implementation of
MVC, in which the model and view(s) are created by some containing class, and then
the model and controller are registered for each view. When the controller for a view
is not specified, the view class creates a default controller for itself automatically.
A Generalized ...