Header Ads

ad

MVC Architecture

MVC (Model-2) Architecture

M ---> Model--->Data (Business + persistence logic)
V ---> View ----> Presentation logic
C ---> Controller -->Integration logic

>> It supports layered application development having clean separation between logics

>> Suitable for medium scale and large scale application.


MVC-1 Architecture


>> In MVC-1 Architecture single web component(Servlet/JSP) is used as Controller and View means Single component contains both Controller and View logics.

>> Here we use multiple technologies to develop logics in multiple layers

>> Compare to Model-1 Architecture it gives more clean separation between logics. 

>> Since single component acts as Controller and View, their logics are mixec. So, if you want more clean separation, go for MVC-2 architecture.
MVC -1 Architecture Diagram


MVC-2 Architecture

>> Here we use multiple technologies to develop logics in multiple layers

>> It is industry standard.

MVC-2 Architecture
MVC-2 Architectue














Advantages of MVC-2 Architecture

>> Since multiple layers are available, the clean separation between logics is possible

>> The modification done in one layer does not affect other layers

>> Maintenance and enhancement of the project is quite easy 

>> Parallel development is possible, So the productivity is good

>> Middleware services can be applied from Spring, EJB kind of technologies.



For any MVC-2 based web application, it is mandatory to follow certain principles which are given below.

MVC-2 Architecture Principles | Rules

>> Every layer is designed to have specific logic. So, place only those logics and do not place any additional logics.

>> There may be multiple multiple view components, multiple model components but it is recommended to take only one controller component either as Servlet component or Filter component.

>> All the activities/communications of the application must take place under monitoring or controlling of Controller servlet.

>> View component should not talk with Model component directly and vice-versa i.e they need to communicate through controller servlet.


Difference between MVC-1 and MVC-2


There is only one difference. In MVC-1 single component is used as Controller and View so view an controller logics are mixed. But in MVC-2, there is separate component for View and separate component for Controller is taken. So, MVC-2 gives more clean separation between logics.


Can we change the roles of various component in MVC2 architecture developement ?

Yes, we can do but it is not recommended.

If we take Servlet component as View

>> Presentation logics mean Html codes and writing Html code in Servlet is quite complex but in JSP it is very easy.

>> The presentation logics of View component change time to time but the modification of logics in servlet is very complex and same modifications are quite easy in JSP components. So, it is recommended to take JSP as View component.


If we take JSP as Controller

Controller must talk with model component like Spring, EJB using Java code but writing java code in JSP is bad practice or developing custom tags representing that java class is also complex. To overcome these problems use Servlet or Filter as Controller.

EJB/Spring/Hibernate component as View and Controller

This thought itself wrong because these component can not take request and can not handle responses but View and controller should take request and handle response. So, prefer Servlet, JSP as controller and view component.

Servlet/JSP as Model component

If we place business logics or persistence logics in Servlet/JSP, we are having following limitations:

>> Logics becomes technology specific logics
>> Logics are not reusable i.e they can only be accessed in java web application and can not be accessed from Android Apps, Standlone Apps etc.

To overcome above problem, use Java classes, Spring components, EJB components to Model layer.




1 comment: