Tuesday, 8 September 2015

MVC Design Pattern

MVC Architecture

The above diagram shows the MVC Architecture.

Implementation of MVC Architecture

The below diagram shows the detailed view of MVC architecture.

Benefits of MVC

  • Clarity of design
    • easier to implement and maintain
  • Modularity
    • changes to one don't affect the others
    • can develop in parallel once you have the interfaces
  • Multiple views
    • games, spreadsheets, powerpoint, Eclipse, UML reverse engineering, ….

The MVC architecture used by web - based applications is further classified into the following types:

MVC Type – I

Here the view and the controller exist as one entity. In terms of implementation, in the page centric approach the controller logic is implemented within the view i.e. with JavaEE, it is JSP, for all the tasks of the controller like extracting HTTP request parameters, the business logic can be called ( implemented in JavaBeans, if not directly in the JSP), and handling of the HTTP session is embedded within JSP using scriptlets and JSP action tags.

MVC Type – II

The Type – I has the limitations of its lack of maintainability. With controller logic inside JSP using scriptlets, the code can get out of control very easily. Hence, to overcome the problems of maintainability and reusability, the controller logic can be moved into a servlet and the JSP can be used for its real implementation – the view component. Hence, using controller logic within a servlet, the MVC type – II design pattern can be implemented.

The Major difference between type – I and type – II is where the controller logic is embedded in JSP in type – I and type – II it’s moved to servlet.

Introduction to MVC

In programming, there is a term called “design patterns” that allow the developers to share their problems and solutions that benefit everyone. A design pattern is capable to identify the problem’s definition and context, a possible solution, and the solution’s consequences.
Christopher Alexander, the initiator of the term “design pattern” in software engineering has stated that each pattern is divided into 3 part rule which expresses a relation between a certain context, a problem, and a solution. Hence, Design patterns provide reusable solution to recurring problems when developing software within a particular context.
Here, the Java Enterprise Edition uses the MVC (Model – View – Controller) as a design pattern used in most of the enterprise applications.
The MVC architecture divides the web – based application into 3 parts: the Model, the View and the Controller.
The pattern used separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes.
Model : defines the lowest level of the pattern maintains data, data representation and business logic.
View : displays all or a portion of the data to the user and user input.
Controller : controls the interactions between the model and view using software code and to dispatch requests and control flow.
Most Web-tier application frameworks use some variation of the MVC design pattern, The MVC (architectural) design pattern provides a host of design benefits.


The Model's responsibilities
  • Provide access to the state of the system
  • Provide access to the system's functionality
  • Can notify the view(s) that its state has changed


The view's responsibilities
  • Display the state of the model to the user
  • At some point, the model (the observable) must registers the views (the observers) so the model can notify the observers that its state has changed.


The controller's responsibilities
  • Accept user input
    • Button clicks, key presses, mouse movements, slider bar changes
  • Send messages to the model, which may in turn notify it observers
  • Send appropriate messages to the view