Most Flex and Flash developers immediately think “MVC” when they here the term “Design Patterns”, and this makes perfect sense considering most of us have either been using MVC for a long time or we have construed our own variations of it’s implementation. What ever the case it is important to understand the basic concepts and implementation of Model-View-Controller.
Model-View-Controller falls under the category of what is known as a compound pattern, that is, a combination of several different Design Patterns to create a named solution. The patterns that make up MVC are typically the Observer Pattern (Model), Strategy Pattern (View and Controller) and the Composite Pattern (View). MVC originated in the Smalltalk-80 language back in the 80’s and it has since been implemented by just about every programming language that requires a user interface, so it is a perfect solution for building Flex Applications.
MVC is even easier to implement in Flex Applications as you can utilize Event dispatching to simplify invocation between the MVC triad. This allows developers to remove the somewhat awkward Observer pattern which is dependent on passing references of each member throughout the MVC triad.
The MVC paradigm is a way of breaking an application, or more commonly, just a piece of an application’s interface, into three parts: the model, the view, and the controller. Each member is intentionaly segregated and represented by an object that is responsible for handling a specific task within the application.
MODEL: The model is used to manage information and notify observers when that information changes.
VIEW: The View is responsible for rendering the model to the user. This is usually rendered as a visual display but a view may also be a sound, a controller vibrating or anything that needs to be conveyed to the user.
CONTROLLER: A controller accepts input from the user and instructs the model and view to perform actions based on that input. In effect, the controller is responsible for mapping end-user action to application response. For example, if the user clicks the mouse button, the controller is responsible for determining how the application should respond.
Here is a basic example of how to implement MVC:
Imagine that you are building a simple application called “Blogs that I read”, which is intended to allow users to click a link that loads a Blog that you read. To implement the MVC Pattern in it’s most basic form you would have three classes; BlogLinksModel, BlogLinksView and BlogLinksController. BlogLinksModel would store the blog links and handle which URL to load when invoked by the controller. BlogLinksView would handle rendering the visual display based on data provided by the model. The view would also be responsible for notifing the controller when a link was clicked. BlogLinksController would handle the processing of the selected link, determining which link was clicked and invoking the model based on this information. The model in turn would then load the URL when invoked by the controller.
It is important to keep in mind that there are many different interpretations of how to implement the MVC pattern. You can keep it simple or you can be as granular as you want. It is basically up to you as the developer to decide which implementation is appropriate for your application. What is important is that you stick to the basic principles of MVC and keep the model, view and controller segregated. Personally I prefer to have the controller do most of the heavy lifting. As in the example above, I prefer to have the controller actually load the URL as oppossed to the model making the request directly. But that’s just me. You can implement MVC according to your own interpretation. As long as you stick to the fundamental rules you’re application will be in good shape. And you will definetly begin to realize the benefits of implementing MVC as your application scales.
I have provided links to the above sample application, ASDoc and source code as well.
If you would like to learn more about MVC click here.