[thelist] Help me think in MVC

Lee kowalkowski lee.kowalkowski at googlemail.com
Mon Dec 11 06:57:29 CST 2006


On 08/12/06, Joe Crawford <joe at artlung.com> wrote:
> I discovered the Model-View-Controller pattern earlier this year.
> Well, not discovered, I had read about it in OO programming articles
> years ago.
>
> I think the reason I never got it was because I'd never encountered a
> system that used it very well, or forced you to use it.

I don't mind, but when I learned MVC, it wasn't an OO topic, it was
more relevant to programming graphics / GUI applications.  I don't
think OO can take the credit for MVC, perhaps those articles are yet
more on the topic "let's show how to do MVC using OO".

When I was a graphics programmer writing pilot displays 10 years ago,
MVC was the only real option (well, if you wanted to write
maintainable code).

Imagine a computer game, you want any single frame to represent the
exact same snapshot in time.  We can't allow changes to the model to
influence a view during its rendition, or it could be distorted.  MVC
doesn't solve this, but it helps identify where you ought to solve it.

The model is just an encapsulation of the real-world information,
how/wher-ever it is stored.  The view is responsible for rendering the
model.  The controller is pretty much responsible for everything else,
like handling input, informing the model of changes, invoking the
view.

The model and view are usually pretty straight forward, yet in the
real world it's not unusual to encounter some overly complicated
varieties, with systems full of layers that essentially populate one
type of object with the values from another, abstraction for the sake
of it, or "just in case".

In a web environment, it can be a tricky job to think in MVC, because
potentially all three are distributed, or there are several and they
are chained.  OO text-books tend to contain trivialised
conceptualisations.

Chaining MVC systems typically means one system uses another system's
view for its model.  So whether something is a model or a view now
depends on context.  You even get views in a database.

You could have the understanding that on the server, the generated
HTML document is the view, but from the client-side perspective this
is the model, the browser will no doubt be rendering the view of the
model by applying CSS and the browser's default rendering rules.

You can have several different controllers in a web application;
JavaScript (especially in AJAX applications), the user's browser, the
HTTP server, your server-side code that receives the requests.

It looks like a right mess, but principally, any system can be defined
by its input, process and output, MVC is just a different approach to
system breakdown.

-- 
Lee



More information about the thelist mailing list