ActionScript 3.0 Design Patterns

(Chris Devlin) #1

442 | Chapter 12: Model-View-Controller Pattern


// composite view
var rootView:CompositeView = new RootNodeView(model,controller, this.stage);

// add child leaf views
rootView.add(new CharCodeLeafView(model));
rootView.add(new AsciiCharLeafView(model));

// register view to receive notifications from the model
model.addEventListener(Event.CHANGE, rootView.update);

Note that only the root view registers with the model to receive update events.


Because the root view is a composite view, the event cascades down to each of its


child nodes. Now whenever a key is pressed, one child node will trace the character


code and the other will trace the ASCII character of the corresponding key. Even


though the nested view structure in our minimalist example was simple, this struc-


ture can work well for nested views with many components.


In our minimalist example, the client built the nested view structure. However, the


build statements for the nested view could have been embedded in theRootNodeView


class, further encapsulating implementation. Now the root view can dynamically add


and remove child views based on application state and user mode. This allows the


user interface element of an application to gain some very powerful capabilities.


Key Features of the MVC Pattern


The primary usefulness of the MVC pattern is the flexibility it affords when creating


applications that have user interfaces. The pattern separates the model, view, and


controller elements and leverages the observer, strategy, and composite patterns to


decouple them.



  • MVC consists of three elements called the model, view, and controller that sepa-
    rate the responsibilities of an application with a graphical user interface.

  • The relationship between models and views is that of a concrete subject and a
    concrete observer in an observer pattern.

  • The relationship between views and controllers is that of a context and concrete
    strategy in a strategy pattern.

  • Multiple views can register with the model.

  • Views can be nested using the composite pattern to create comple xuser inter-
    faces that streamline the update process.

Free download pdf