Main Page | See live article | Alphabetical index

Model view controller triad

The \'Model-View-Controller' is a design pattern in programming. It is most commonly thought of in the context of Graphical User Interface (GUI) construction.

The event-oriented nature of most modern GUIs is characterized by complex event handling. Each time the mouse moves, a button is clicked, or a key stroke is input, one or more events are generated. The code to handle these events at the lowest level is complex and difficult to write.

In contrast, objects in object oriented programming are composed of methods and data members that attempt to model something from the domain of interest. The best models are fully encapsultated, meaning that they implement every aspect of that real world object of interest to the domain of interest, and that they don't have any extra code that doesn't model the real world. Encapsulation also means that the class is implemented in such a way that the internal representation of data is not exposed to the user of the object.

The impedance mismatch between these two worlds, that of events, and that of objects, is something that's been recognized for a long time. Getting these two worlds to cooperate requires a lot of 'glue code' to make them work together.

The model-view-controller model introduces the controller object in between the view (the GUI class) and the model (the object) to communicate between the other two objects. The actual implementation of the controller object can vary quite a bit, but the idea of an object to 'transform' events to changes in data and execution of methods is the essence of this pattern.

Attempting to stitch these two worlds together in a hand coded method without architecture is very common, and results in the model object being polluted with knowledge of the interface, and vice-versa. This makes the code very inflexible and difficult to maintain. For this reason (among other), many programming shops develop the user interface design early in the process of design, and freeze the interface early. The unfortunate side effect of this is that the domain of the problem often isn't clearly understood by the programmers until late in the implementation process. Thus, just at the time that the developers are finally competent to create a good interface, they are kept from changing it. MVC models allow the code to be more flexible later in the development process, allowing for changes that make sense at the time it makes sense to make them.

The first implementation of the model-view-controller to be widely known was introduced in the Smalltalk language in the early 1980s.