Model – View – Controller (MVC)

MVC is very old and very famous design pattern. It is used in web and desktop applications, in different programming languages (C++, Java, .NET, Python, etc.).

First I read about it in Design Patterns. Elements of Reusable Object-Oriented Software and since that time I have heart a lot of things about it and see a lot of realization of this pattern.

I decide to write this post thanks to statement of my good friend – he said that MVC is not used in Java web frameworks. Thus I want to show that it is used and MVC realization in Java is not worse than in Python/Django.

So let’s start from small review of MVC pattern.

The following diagram illustrates main concept of MVC pattern.

Here model is object, which contains information about domain. Model does not have visual interface and it contains all data, which does not link with visual interface. So it may be domain object or script.

View is responsible for showing data of model using UI mechanisms.

All changes of data should be handled by Controller, which receive data, make all necessary operations and refreshes view. So in this case graphical interface consists of view and controller. Thus division between view and controller is not so important and therefor sometime it is not visible.

Dependency between view and model is much more important, because they belong to different areas of software development. Because when you’re working on view you should care about user interface and its usability, but when you’re working on model you should concentrate on business logic. Moreover there are cases when the same information should be shown by different ways.

The following table shows model, view and controller in different web frameworks:

GWTDTOs, which are placed in common partCustom and standard widgets and composition of themListeners and callbacks within servlet-based services
Spring MVCDTOs, which are placed in Spring Model classJSPSpring Controller classes
JSFDTOs, which backing beans returnJSPBacking beans
Python/DjangoJSON-based DTOsTemplateSet of request handler methods
Also notes that some frameworks need additional JavaScript-based mechanisms, which are responsible for processing AJAX calls. These mechanisms are also part of controller.

So as you can see Java web frameworks actively and successfully use MVC pattern.

May be my friend mean that we did not use RESTful services together with JavaScript libraries like jQuery or DOJO, but it is another story and I’d to describe it in one of next posts.