Truckin’ Through ActionScript 3.0 MVC: Part I—Structures and Materials

Truckin' thru MVC

Truckin' thru MVC

First off, let me say that Chandima is the go-to guy when it comes to the Model-View-Controller (MVC) framework, including PureMVC. So if you’re interested in MVCs, I’d suggest you start where I did—Chapter 12 in our book and any of Chandima’s posts on PureMVC. Further, if you have GoF’s original Design Patterns you might want to look up the several references to the MVC there, especially the initial explanation of its purpose in Chapter 1. Finally, by way of introduction, last year about this time, I broached the query, Is MVC Obsolete? based on a post by Brian Lesser. That kicked up very little dust either on our blog or Brian’s, but it helped to take a closer look at the MVC. You’ll be relieved to hear that I’m not going to trash MVCs in this series. Rather I want to explore the MVC as a compound pattern (or perhaps framework) to see some different things we might do with it.

MVC in a Nutshell: The Great Decoupler

To get started, GoF show the MVC to be three distinct components in what they call a a triad of classes. As such, they discuss its elements in terms of objects. The application object is defined by the Model, the screen presentation by the View and the way the way the user interface reacts to the user input by the Controller. Prior to MVC, the application, its appearance on the screen and the UI were grouped together in a single class. By decoupling the objects into separate units, the MVC loosens the relationship following a key good practice. (I used to cram them all together in a single class thinking that I was tightening up my code—how dumb was that?) As a first image of the MVC, then, think of it as a tool to loosen up code.

Metallurgy and Structural Engineering

Every now and again, I see the perp walk of a contractor who’s been arrested for using inferior materials on a public structure. In one case, the contractor increased his profits by using cheaper steel beams in a bridge he built. At the trial the prosecution proved that the beams were substandard by the expert testimony of a metallurgist. The metallurgist compared the beams used in the bridge with those that met the building standards and found the ones used by the contractor to be substandard.

Structural engineers will tell you that the strongest structure in construction is a triangle. That’s why you see so many triangles in bridge structures. While studying at Cambridge, I visited the wooden bridge at Queen’s College known as the Mathematical Bridge—lots of triangles in its structure. The bridge was originally built in 1749 and then rebuilt in 1866 and and again in 1905. Given that history and the fact that it’s made of wood says more for the structure than it does for the building materials.

Figure 1 shows two different bridge structures. The top structure shows the bridge held up by beams arranged in a square that fail when too much pressure is placed on the bridge. The bottom structure shows triangles used to hold up the bridge withstanding pressure:

<em><strong>Figure 1:</strong>Structure is independent of the materials that make up the structure</em>

Figure 1: Structure is independent of the materials that make up the structure

If you look at a problem from the viewpoint of a structural engineer, you tend to see how the different elements relate to one another. If you look at the problem from the view point of a metallurgist, you tend to look at the composition of the materials. Think of the beams as ActionScript 3.0 and algorithms, and think of the design patterns and the MVC as the structures arranging the beams.

Ever since the advent of ActionScript 3.0, a lot of us have been very happy with it as the basic construction material of our programs. Earlier versions of ActionScript were unsatisfactory in many ways, and while ActionScript 3.0 is far from perfect, it is light years better than the original. So unlike the contractor who is being frog walked into court for using inferior materials, we’ve got very good materials. The problems arise when developers fail to use good structures. Even the best materials will fail without good structure as Figure 2 shows.

Confusing Materials and Structures: The Drunk’s Search

An old joke tells the story of a drunk who has dropped his keys in a dark doorway. He staggers out to a street light and proceeds to look for them. A police officer comes by and asks the drunk what he’s doing. The drunk explains that he has dropped his keys in the dark doorway and he’s looking for them. Confused, the police officer asks, “Well why are you looking here under the street light instead of the doorway where you dropped them?” The drunk looks up and says, “Because this is where the light is!

The point of this story is that we often look to ActionScript, it’s syntax, methods, events, and properties because we understand it better. Instead of looking at the structures of our programs, we focus on getting something to work because for many of us, ActionScript is where the light is!. We’ve spent years trying to understand and use ActionScript 3.0, and we tend to define problems and solutions in terms of that understanding. The MVC is a great place to break off from our focus on the materials of ActionScript 3.0 and to re-focus on the structures we are putting our ActionScript 3.0 into. By breaking up the application, screen presentation and user interface, we have to re-think what we’re doing with our code. We have to use it differently.

Now, lest we think that we can work with structure while ignoring materials, let me disabuse you of that notion. The corrupt contractor who uses inferior materials (algorithms) is going to build an inferior bridge, no matter how good his structure is. It’ll hold up, but sooner or later, the materials fall apart along with his well-structured bridge. Likewise in programming, we must attend to our algorithms, but like the language itself, they must be placed in the best possible structure.

To give you an example, PHP is not a typed language—the data are weakly typed. Yet you will find plenty of PHP design patterns. In fact IBM sponsored a whole series of PHP design patterns. I’ve never understood how you can follow one of the first principles of design patterns, type to an interface; not an implementation, using PHP. Given that PHP and ActionScript 3.0 are different materials is only important insofar as syntax and symbols require different ways to implement design patterns. The classes in the design patterns have the same relationship, however. So if you understand the design patterns in one, it’s not so difficult to understand them in the other. In the same way that your can use bamboo or steel I-beams in bridge construction with the same structure, the details of using the different materials vary independent of the structures.

Selecting Design Patterns for the MVC

In the next installment of this discussion of the MVC, I’d like to look at the possible design patterns we might use. We know that to select a design pattern for a task, we must ask, What varies? However, since we are in a position to select multiple design patterns as part of a larger pattern or framework, what are the questions we need to ask and answer to accomplish a goal where we use an MVC?

4 Responses to “Truckin’ Through ActionScript 3.0 MVC: Part I—Structures and Materials”


Leave a Reply