
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
400
|
Chapter 18: The Model-View-Controller Design Pattern
The ClockModel Class
The ClockModel class extends Observable. It provides methods for setting the time
and runs an internal ticker that updates the time once per second. Whenever the
time changes, the ClockModel class uses notifyObservers( ) to broadcast the change
to all registered views.
Here are the properties defined by the ClockModel class. Remember that the model
class’s properties represent its current state.
hour
The current hour, from 0 (midnight) to 23 (11 p.m.)
minute
The current minute, from 0 to 59
second
The current second, from 0 to 59
isRunning
A Boolean indicating whether the clock’s internal ticker is running or not
Here are the public methods of the ClockModel class. These methods are used by the
ClockController class and the Clock class to manipulate the clock’s data:
setTime( )
Sets the clock’s hour, minute, and second, then notifies views if appropriate
start( )
Starts the clock’s internal ticker and notifies the views
stop( )
Stops the clock’s internal ticker and notifies the views
Notice that the ClockModel class does not define a reset( ) method, despite the fact
that the clock’s user interface has a Reset button. Instead, the ClockController class
defines a resetClock( ) method, used to set the time to midnight. ...