Home

 

Model-view-controller (MVC) pattern

The model-view-controller (MVC) concept is a pattern used many times when describing and building applications with a user interface component, including Java EE applications.

Following the MVC concept, a software application or module should have its business logic (model) separated from its presentation logic (view). This is desirable because it is likely that the view will change over time, and it should not be necessary to change the business logic each time. Also, many applications might have different views of the same business model, and if the view is not separated, then adding an additional view causes considerable disruptions and increases the component's complexity.

This separation can be achieved through the layering of the component into a model layer (responsible for implementing the business logic) and a view layer (responsible for rendering the user interface to a specific client type). In addition, the controller layer sits between those two layers, intercepting requests from the view (or presentation) layer and mapping them to calls to the business model, then returning the response based on a response page selected by the controller layer. The key advantage provided by the controller layer is that the presentation can focus just on the presentation aspects of the application and leave the flow control and mapping to the controller layer.

There are several ways to achieve this separation in Java EE applications, and various technologies, such as JavaServer Faces (JSF) and Struts, have different ways of applying the MVC pattern. Our focus in this chapter is on JSPs and servlets that fit into the view and controller layers of the MVC pattern. If only servlets and JSPs are used in an application, then the details of how to implement the controller layer are left to whatever mechanism the development team decides is appropriate, and which they can create using Java classes.

In one example presented later in this chapter, a Command pattern (see Design Patterns: Elements of Reusable Object-Oriented Software in the bibliography) is applied to encompass the request to the business logic and interactions made with the business logic through a facade class, while in the other interactions the request is sent directly to a servlet that makes method calls through the facade.

ibm.com/redbooks