
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
388
|
Chapter 18: The Model-View-Controller Design Pattern
The General Architecture of MVC
Before we study our specific clock example, let’s explore the general structure of the
MVC design pattern.
Communication in MVC
Although the model, view, and controller classes in MVC are intentionally segre-
gated, they must communicate regularly. The model must send notifications of state
changes to the view. The view must register the controller to receive user interface
events and, possibly, request data from the model. The controller must update the
model and, possibly, update the view in response to user input.
To facilitate this communication, each object in MVC must store a reference to the
other object(s) with which it interacts. Specifically, the model instance needs a refer-
ence to the view instances that render it, while the view and controller each needs a
reference to the model and reciprocal references to each other. Figure 18-1 shows
how the objects in MVC reference one another. The diamond shape in the figure rep-
resents a composition relationship, in which one object stores an instance of another.
Communication proceeds in a single direction (as shown in Figure 18-2) through the
object references shown in Figure 18-1, as follows:
1. The view receives user input and passes it to ...