
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
An MVC Clock
|
419
The ClockController class is fairly generic. It could, in theory, be extended to provide
event handling for other clock-related classes as well. For example, if the
ClockAnalogView class allowed its hands to be dragged, methods could be added to
ClockController to handle the clock-hand drag events. In that case, each view would
have its own instance of ClockController. Alternatively, separate controller classes could
be created to handle the drag events. Or, the ClockTools class and the ClockAnalogView
class could use different controllers that inherit from a single ClockController super-
class, which would define a set of methods common to both classes.
Putting It All Together: The Clock Class
The pieces of our MVC clock are now complete. All that’s left to do is assemble them
into a functional application. The final assembly of our application occurs in the
Clock class, our primary class that performs the following tasks:
• Creates the ClockModel instance
• Creates the clock views (ClockAnalogView, ClockDigitalView, ClockTools)
• Registers the clock views to receive updates from the ClockModel
• Optionally sets the clock’s time
• Starts the clock ticking
All the preceding tasks occur in the Clock class’s constructor function. However,
because the clock needs to run