Wednesday, March 9, 2011

The Beauty of MVC

The widely-known Model-View-Controller pattern was published first in 1988 (by Krasner & Pope). It was originally indented to represent user mental models and so on, and while some people may want to superseed it with new paradigms, today I want to talk about its beauty.

Because MVC not [only] models the user's mental model, it models the world.

It's nothing else, than a practical implementation of Hegelian dialectic.

Let's start with a simple scheme. The user sits besides a computer. [S]he looks at the monitor, while trying to achieve something by pressing buttons on the keyboard, or trying to wander around with a mouse or on a touchpad.

Generally, we don't really distinguish between the different input peripherials with MVC. The monitor is the output, other human-computer interface peripherials are for input, it's that easy:



(Based on image of flickr user ntr23 under Creative commons license)



Now let me exchange the visual vocabulary to a much more simplified one:




Ok, so, introduce MVC:




All the inputs are handled by the controllers, and all the outputs are handled by the views :)

Now what we should understand, that a model is an abstract representation of a system.

Also, everything outside a system - which the system has no effect for - are - actors!

Do we realize, that all the externalities of the system can be modeled just by MVC?

Now let us just think of it in layers:
  • Browser: you have a javascript MVC application; it's classical MVC
  • AppServer: your controllers are which feed the input to the db layer, your views are your templates (or JSON output)


(Actually the second one greets us back from Web MVC frameworks like Rails, Zend or Symfony)

These are classical, but:
  • DB layer: your controllers are your API facade; your views are your DB abstractions
  • External system: your views are what you provide the system with (like, XML protocol), your controllers are what you expect from the system


The philosophical ingeniusness of MVC is that in terms of human-computer interaction, the human is the only externality to the system; also that inputs and outputs of a system should be modeled separately, but with the same terms: all outputs should be modelled as views and all inputs as controllers, regardless what they are.

And given that in its essence, it only speaks about a system in interaction with its environment, it can be applied recursively to the layers of the system.

I think and hope that this such a genuine idea has still a lot of years with us.

No comments:

Post a Comment