cristobal baray
kids iPhone apps
s100+ pages

Last edited - March / 99


-- Hear and forget; see and remember; do and understand...

the model-view-controller (MVC) design pattern

hereadvantagesexample appmodelscontrollersviewstodosrefs

MVC introduction

The main idea behind design patterns is to extract the high level interactions between objects and reuse their behaviors from application to application. They were inspired by an architect, Christopher Alexander, discussing the way that architectural design has patterns of its own:

Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.

A Timeless Way of Building

In theory this sounds great, but I've found in practice it takes some getting used to. Admittedly, it might take longer for me cause I'm a (reforming?) guerrilla programmer (the error in my ways has caught up with me and I'm taking steps towards programming for people - not computers). Sure, I've found that the design patterns are useful, but their advantage is not only in creating re-usable, easy to understand code - but, more importantly in helping me clarify the way that I think about programs. I find this second advantage much more substantial.

Writing code is usually the easy part - the hard part is figuring out exactly what code to write. Time spent carefully thinking about the code to write will save enormous amounts of time and effort when it comes to actually writing the code. Still, many people don't always work that way - myself included. Design patterns are helping me shape the way I parse a problem, which leads to helping me break up the problem into objects and modules. The Model View Controller pattern has probably changed my style and efficiency the most. It has lead to new ways of writing entire applications, constantly improving my applications in usability. Yet, while I was learning about the pattern, I was surprised at how hard it was to find information about it (some of the more useful bits of info I found are included in my references page - though Head First has become my favorite learning tool). MVC diagram

So, I decided to add a couple of pages about the pattern myself. I want to share some info about the pattern that I've found so useful but seems to get so little press. And I figured it would also clarify it even more in my own head. One little disclaimer - you might notice differences between what I present and what others present. The differences are representative of our styles I suppose - because I've made adjustments along the way and have changed things. I've tried to mention these areas when they pop up.

The MVC pattern is surprisingly simple, yet incredibly useful. Essentially, the pattern forces one to think of the application in terms of these three modules -

  • Model : The core of the application. This maintains the state and data that the application represents. When significant changes occur in the model, it updates all of its views
  • Controller : The user interface presented to the user to manipulate the application.
  • View : The user interface which displays information about the model to the user. Any object that needs information about the model needs to be a registered view with the model.
This application architecture is very similar to a client/server model, except that all the components are bundled into one application. (But can be distributed later...) So, what are some of the advantages then?

hereadvantagesexample appmodelscontrollersviewstodosrefs