
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
404
|
Chapter 18: The Model-View-Controller Design Pattern
Notice that our ClockModel class defines methods for setting the current time but
does not define any methods for retrieving it. That’s because the clock’s time is
pushed to all views via a ClockUpdate object; views do not need to query the state of
the model in our example. However, we could just as sensibly have required views to
retrieve the new time from the model. Typically, the push system (in this case, send-
ing the time) is used if pulling information from the model (in this case, retrieving
the time) is too processor intensive. That limitation doesn’t apply here, so both the
push and pull systems are appropriate.
The ClockUpdate Class
The ClockUpdate class is a simple data holder used to transfer the state of the
ClockModel to views when the time changes or the clock is started or stopped. It
defines four properties—
hour, minute, second, and isRunning—which are accessed
directly. To avoid distracting from the issues at hand, we won’t implement accessor
methods such as getMinute( ) for these properties. The ClockUpdate class is simply a
data vessel, used like an associative array or a hash, so the direct property access is
arguably appropriate.
}
/**
* Makes time pass by adding a second to the current time.
*/
private ...