ActionScript 3.0 Design Patterns

(Chris Devlin) #1

428 | Chapter 12: Model-View-Controller Pattern


Model


The model is responsible for managing the state of the application. The application


logic in the model performs two important tasks: it responds to requests for informa-


tion about the state of the application, and takes action on requests to change the state.


View


A view is the external face of the application. Users interact with the application


through the view. An application can contain multiple views that can be both inputs


and outputs. For example, in the case of a portable digital music player such as an


iPod, the screen is a view. In addition, the buttons that control song playback are


views as well. The screen shows the name of the current song, song duration, album


art, and so on, that communicate the current state of the device. Views don’t neces-


sarily have to be visual. In the case of a digital music player, the sound that comes


through the headphones represents a view as well. For example, clicking a button


may provide some auditory feedback in the form of the click sound. Changing the


volume is reflected in the audio output as well. The auditory feedback corresponds


to the state of the application.


Controller


Although the term controller implies an interface that controls an application, in an


MVC pattern, the controller does not contain any user interface elements. As pointed


out previously, user interface elements that provide input belong to theviewcompo-


nent. The controller determines how views respond to user input.


For example, our digital music player hasvolume upandvolume downbuttons in a


view. The sound volume of the device is a state variable. The model will hold this


variable with the necessary application logic to change it. If the sound volume range


is 0 to 10, the controller determines how much the volume should go up or down


with a single click on the volume up and down buttons. The controller can tell the


model to raise the volume by 0.5 or 1.0, or any value, programmatically. In this


sense, controllers are specific implementations that determine how the application


responds to user input.


Although each element in the MVC triad has separate and unique responsibilities,


they don’t function in isolation. In fact, in order to be an MVC pattern, each element


needs to communicate with one or more elements. That is what we’ll look at next.


Communication Between the MVC Elements


Each element in the MVC pattern communicates with each other in very specific


ways. Communication is necessitated by a sequence of events that are generally

Free download pdf