10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Model<br />

The model is responsible for managing the state of the application. The application<br />

logic in the model performs two important tasks: it responds to requests for information<br />

about the state of the application, and takes action on requests to change the state.<br />

View<br />

A view is the external face of the application. Users interact with the application<br />

through the view. An application can contain multiple views that can be both inputs<br />

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

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

views as well. The screen shows the name of the current song, song duration, album<br />

art, and so on, that <strong>com</strong>municate the current state of the device. Views don’t necessarily<br />

have to be visual. In the case of a digital music player, the sound that <strong>com</strong>es<br />

through the headphones represents a view as well. For example, clicking a button<br />

may provide some auditory feedback in the form of the click sound. Changing the<br />

volume is reflected in the audio output as well. The auditory feedback corresponds<br />

to the state of the application.<br />

Controller<br />

Although the term controller implies an interface that controls an application, in an<br />

MVC pattern, the controller does not contain any user interface elements. As pointed<br />

out previously, user interface elements that provide input belong to the view <strong>com</strong>ponent.<br />

The controller determines how views respond to user input.<br />

For example, our digital music player has volume up and volume down buttons in a<br />

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

variable with the necessary application logic to change it. If the sound volume range<br />

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

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

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

sense, controllers are specific implementations that determine how the application<br />

responds to user input.<br />

Although each element in the MVC triad has separate and unique responsibilities,<br />

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

needs to <strong>com</strong>municate with one or more elements. That is what we’ll look at next.<br />

Communication Between the MVC Elements<br />

Each element in the MVC pattern <strong>com</strong>municates with each other in very specific<br />

ways. Communication is necessitated by a sequence of events that are generally<br />

428 | Chapter 12: Model-View-Controller Pattern

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!