436 | Chapter 12: Model-View-Controller Pattern
done is to instantiate the model, view, and controller classes. Example 12-6 shows
the Flash document class that instantiates the MVC elements.
After the model, controller, and view are instantiated, they’ll communicate with each
other and work. Clicking a key on the keyboard will result in the key code for that
key being printed in the output panel.
You have to disable keyboard shortcuts to test for key presses. Other-
wise the Flash user interface intercepts certain key press events that
match keyboard shortcuts. To disable keyboard shortcuts select Dis-
able Keyboard Shortcuts from the Control menu when your Flash
movie is playing.
Note that the model instance is passed to the controller. Similarly, the model and
controller instances are passed to the view as well. We can easily substitute different
models and controllers on the condition that they implement the IModeland
IKeyboardInputHandlerinterfaces. Additional view elements can also be added with-
out disruption due to the subject-observer relationship between the model and view.
The model doesn’t know about views as it’s the responsibility of the view to register
itself to receive update notifications from the model. This is the beauty of the MVC
pattern; the model, view, and controller are separate, loosely coupled elements that
allow for flexibility in their use.
Nested Views as Leaves and Nodes of a Composite Pattern
You may remember that the view is arguably the most comple xelement in the MVC
triad because it participates in both the observer and strategy pattern
Example 12-6. Main.as (document class for minimalist example)
package
{
import flash.display.;
import flash.events.;
/**
- Main Class
- @ purpose: Document class for movie
*/
public class Main extends Sprite
{
public function Main( )
{
var model:IModel = new Model( );
var controller:IKeyboardInputHandler = new Controller(model);
var view:View = new View(model, controller, this.stage);
}
}
}