01 - Introduction to MVC Architecture

Introduction

  • Client server communication takes place in a specific pattern which is known as application design pattern or architecture. The application is developed according to different modules specified in architecture. Different types of architecture can be:

MVC Architecture, Visitor architecture, state architecture, observer architecture etc.

  • J2EE (i.e. Enterprise Java applications such as Spring, Struts, Hibernate, JDBC integrated framework) follow MVC architecture with different tier models. MVC is implemented in J2EE using following models:
    • Single-tie or one-tier architecture
    • Two-tier architecture
    • Three-tier architecture
    • N-tier architecture

 

MVC Architecture

  • MVC is an abbreviation for Model-View-Controller.
  • For ease of development and deployment of the project, the web-application is divided into basic 3 modules: model, view and controller.
  • As the name suggests:
    • Model: responsible for handling database interaction with the controller (database layer)
    • View: responsible for displaying contents of user (presentation layer)
    • Controller: responsible for acting as per user action (business logic layer)
  • Considering the example of AWT or Java Swing (Drop-down menu/Combobox menu), if the user wants to retrieve the states belonging to the selected country from drop-down menu,
    • Model: retrieves the selected names of states from the database
    • View: displays the Combobox menu to the user (coordinates with model)
    • Controller: sends message to model about the user’s selection of country name.
  • Considering Struts or Spring or Hibernate frameworks:
    • Model: Java Beans (database)
    • View: HTML or JSP page
    • Controller: back-end servlet (converted JSP’s)

 

  • The complete architecture is mainly controlled by the controller. The end-user (client) actions on view, based on which the controller delegates the requests to model. The model retrieves required data from beans or database (through database queries) and responses back to the controller. Based on the response received, the controller changes/modifies the data in view.

 

  • The figure below represents MVC in client-server:

 

Figure: MVC architecture

 

Practical Scenario: how to use MVC

  • View : It is the presentation layer where all the components (buttons, checkboxes, radiobuttons, textfields, textarea etc) are placed and arranged in a container.
  • Controller:
  • Model:

 

MVC Models:

 

  1. One-tier architecture:

  • The client-server architecture in which every communication element (such as presentation layer, business logic and database layer) is placed in a single unit is termed as one-tier architecture or single-tier architecture.
  • For example:

Figure: One-tier MVC architecture

  • Every unit responsible for client-server interaction is located in one place in the client. The same architecture can be repeated for the server as well as n-number of clients.
  • This type of arrangement is considered to be the simplest and most direct arrangement. The ease-of-access to database layer as well as business application layer increases.
  • The clients or servers following this architecture are also known as thick clients or servers.

Advantages:

  • Better performance: as these layers are inbuilt, there is no issue regarding lack of access or loss of connection etc.
  • More security: database comes embedded in this architecture, i.e., the client who wants to access data and business logic has full rights.
  • Improved scalability: Every application module is closely connected; hence they can be scaled easily.

Disadvantages:

  • As all the three elements are installed on every client, the cost of developing this architecture increases.
  • Maintenance cost also increases.

 

  1. Two-tier architecture:

  • In two-tier architecture, the GUI (view module) is located at client’s side while the business logic (controller) and database (model) are located at server side.
  • The client sends request to server through view module and the application at server side directly communicates with the database and retrieved results are sent back to the client.

The communication between client-server requires constant internet or intranet connection. 

Figure: 2-tier MVC architecture

 

  • To understand more, let’s take an example of online movie ticket booking system. Consider Mr. ABC is reserving tickets for seats A1 and A2. At the same time Mr. XYZ is also reserving tickets for the same day. In this case, the requests coming from both the clients are in queue on the server. If the request coming from client 1 is not responded and simultaneously, request from client 2 is served, the data in the database can be left in an inconsistent state. This may cause data integrity issues.

 

Advantages:

  • Faster Communication
  • As only the GUI is to be maintained at client side, the database maintenance and modification becomes easy.
  • Less cost in developing

Disadvantages:

  • Constant internet or intranet connection is required
  • As the number of clients increases, the server may lag in processing client’s requests.       

Scenario:

MVC for movie ticket booking system:

Figure: Online movie ticket booking scenario: 2 tier MVC

  1. Three-tier architecture:

  • In three-tier architecture, two servers and one client are involved.
  • MVC is divided as:
    • Model : Database Server
    • View : Client Machine
    • Controller : Application Server
  • The client communicates with application server in the form of a request. These requests are served by the application server in form of responses. The responses include retrieval of data from database by the application server.
  • The architecture is:
  • Figure: 3-tier MVC architecture

Scenario:

Figure: MVC 3tier in movie ticket reservation system

Advantages:

  • More security: as the client has no communication with the database, it is more secure as compared to one-tier architecture.
  • Modification of individual modules is possible (for example, modification in the database is possible without affecting other modules).
  • If one of the tier stops working, the other modules can still function properly.
  • More scalable as compared to one-tier and two-tier architecture.

Disadvantages:

  • Increased in complexity
  • Maintenance cost of all the modules increases
  • The separation may lead to performance issues.

 

MVC in Struts:

  • MVC in struts can be differentiated as:
    • Model: Java Beans (that interact with database at back-end)
    • View: JSP (Java + HTML)
    • Controller: Servlet (or filters)

For example:

Figure: MVC Architecture in Strtuts2 Framework

 

 

 

Like us on Facebook