Saturday, November 8, 2008

MVC Pattern & its importance. Push & Pull Mechanism

MVC Pattern & its importance. Push & Pull Mechanism

MVC (Model View Controller) architecture/pattern

It's an architecture (which is sometimes referred to as a design pattern as well) pattern which clearly separates the Business Logic, Presentation of the Response, and the Request Handling & Processing. JSP Model 2 Architecture is based on the MVC pattern.

Does it sound complex? Well... it's not. In fact this pattern aims to make application design, development, and maintenance simpler. The scalability and extensibility of the applications become easier and less time consuming.

MVC & Web Applications

MVC pattern is normally talked about in the context of Web Applications only, but the fact is that it's used for other applications as well. For example: SmallTalk applications which are not Web Apps use this pattern for the simple reason that the use separates the roles and responsibilities (not only among developers, but also among the various application components... can the two happen separately?) very nicely which ultimately results into a robust, simpler, and better maintainable/scalable software life cycle.

Java/J2EE (or .Net) based Web Application development is heavily based on the MVC pattern and it seems the two are inseparable now. Not only design and development, but also the post-production phases like maintenance, scalability, and extensibility are handled so well by this pattern that it has become kind of de-facto architecture for Web Apps.

MVC Components

  • Model: this component is responsible for maintaining the state of the business domain of the application. It can be as simple as a plain Java object populated manually using a ResultSet by obtaining it from a DB connected via JDBC APIs or it can be populated automatically by using one of several ORM tools like TopLink, Hibernate, etc.
  • View: this component is responsible for presenting the business domain using data from the Model. This component is typically a HTML page for static content display or a JSP page for dynamic content display. WML is used as View component for display on wireless and hand held devices. Since the Model and View components are separate, hence we can have two or more Views for the same Model - maybe one for display on PCs and another for wireless & hand held devices.
  • Controller: this component is responsible for all the Request Processing by controlling the flow of control and by instantiating, invoking, and maintaining the business operations. It's normally a Java Servlet. For request handling it needs to intercept he HTTP request, translate the request into some business function, get the function (or the handler of the function) invoked, and finally by sending back the response page to the client. Evidently it separates the presentation of the data with the business function implementation which makes which promotes re-usability of the business function implementations. Since all the requests are processed through Controller only, hence we can put the common code (which we would other wise need to put in every JSP page) in it. This will obviously improve the maintainability as the common code would be required to be looked at only one place and not at multiple places.

Push & Pull Notification Mechanisms

Web applications are stateless as they use the stateless protocol HTTP. So, once a request is served to the client then there is no direct way of notifying him/her about the changes made to the Model. To handle this scenario there are two mechanisms -

Push Mechanism:
forcibly sending the response back to the client whenever any change to the Model happens, but this is not very easy to implement as the server would require to maintain client info. There are other challenges as well. HTTP requests are processed by first establishing the connection to the server, sending the requests, receiving the response, and finally terminating the connection. Once the request has been served the connection is lost. How would the server then send back the updated response? Since the HTTP protocol is stateless the client may simply close the browser window without even notifying the server that it no longer needs any updated info.

Pull Mechanism:
this mechanism is normally used to handle the above mentioned scenario. This requires the client to pull the updated data rather than the server pushing the updated data to the client. How can this be done? Well... either by having a simple button (clicking which will make another fresh request to the server and the client would get the updated data in response) or by having a script which would automatically be making the HTTP requests - maybe periodically after a certain time-interval. This is how Live Cricket Scores are displayed. Right?

Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.


1 comment:

krishanthan said...

Thanks.... great article..