{"id":3632,"date":"2020-06-08T19:42:39","date_gmt":"2020-06-08T14:12:39","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3632"},"modified":"2024-09-24T18:59:13","modified_gmt":"2024-09-24T13:29:13","slug":"what-is-mvc-architecture","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/what-is-mvc-architecture\/","title":{"rendered":"What is MVC Architecture?"},"content":{"rendered":"\n<p>MVC (Model View Controller) is a design pattern that separates business logic, presentation logic, and data. MVC is used to design both web applications as well as mobile applications. It consists of three layers:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Model:<\/strong> It represents the business layer of the application.<\/li>\n\n\n\n<li><strong>View:<\/strong> It defines the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/servlet-vs-jsp\/\">presentation logic of the application<\/a>.<\/li>\n\n\n\n<li><strong>Controller:<\/strong> It manages the flow of the application and behaves like an interface between Model and View.<\/li>\n<\/ol>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"542\" height=\"308\" src=\"https:\/\/lh6.googleusercontent.com\/d1a8Wgb42JIsqZjHZC44a3UNTrf3suxJRHxIvgTgkrB5GaVC7IW21FeX62iAYIyBhgRZHhngt7ITeud0srl4PJed7imTSzxw_IM-8jfrhXmKI5LiwonW6UGS9RUL_YmhCYWpyf-8tMc7UYpYXA\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Model Layer:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Model layer helps in retrieving and storing the state of the model in the database.<\/li>\n\n\n\n<li>It contains pure application data.<\/li>\n\n\n\n<li>It consists of those classes which are connected to the database.<\/li>\n<\/ul>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Course {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private String CourseName;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private String CourseId;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private String CourseCategory;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public String getId() {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return CourseId;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void setId(String id) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.CourseId = id;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public String getName() {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return CourseName;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void setName(String name) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.CourseName = name;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public String getCategory() {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return CourseCategory;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void setCategory(String category) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.CourseCategory = category;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>View Layer:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The View Layer helps in representing the output of the application.<\/li>\n\n\n\n<li>It fetches the data from the Model layer and displays the data to the user when the user asked for it.<\/li>\n\n\n\n<li>It does not interact with the Model layer directly. It gets the information with the help of the Controller.<\/li>\n\n\n\n<li>It also represents data from the diagrams, tables, or charts.<\/li>\n\n\n\n<li>It consists of JSP, HTML, etc. and usually, it represents the UI (User Interface) of the application.<\/li>\n<\/ul>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CourseView {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void printCourseDetails(String CourseName, String CourseId, String CourseCategory){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Course Details: \");\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Name: \" + CourseName);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Course ID: \" + CourseId);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Course Category: \" + CourseCategory);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Controller Layer:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Controller Layer is an interface between the Model Layer and the View Layer.<\/li>\n\n\n\n<li>It receives the requests from the user through View Layer and processes them.<\/li>\n\n\n\n<li>The request is then sent to the Model Layer for data processing. Once it is processed, the data is again sent back to the View layer so that it can be displayed.<\/li>\n\n\n\n<li>It handles user interaction. It <a href=\"https:\/\/www.h2kinfosys.com\/blog\/handling-mouse-click-and-keyboard-event-in-selenium-webdriver-using-actions-class\/\">interprets the keyboard and mouse events<\/a>.<\/li>\n<\/ul>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CourseController {<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private Course model;<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0private CourseView view;<br>\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public CourseController(Course model, CourseView view){<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.model = model;<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.view = view;<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br>\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void setCourseName(String name){<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0model.setName(name);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br>\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public String getCourseName(){<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return model.getName();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br>\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void setCourseId(String id){<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0model.setId(id);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br>\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public String getCourseId(){<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return model.getId();\u00a0\u00a0\u00a0\u00a0\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br>\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void setCourseCategory(String category){<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0model.setCategory(category);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br>\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public String getCourseCategory(){<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return model.getCategory();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public void updateView(){\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0view.printCourseDetails(model.getName(), model.getId(), model.getCategory());<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0\u00a0\u00a0<br>\u00a0\u00a0\u00a0\u00a0}<br><br><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advantages of MVC Architecture:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigation control in MVC is centralized.<\/li>\n\n\n\n<li>It becomes easy to maintain the application designed using MVC Architecture.<\/li>\n\n\n\n<li>It provides the reusability of code. The maintenance of code becomes easy.<\/li>\n\n\n\n<li>It helps in avoiding complexity.<\/li>\n\n\n\n<li>It supports <a href=\"https:\/\/en.wikipedia.org\/wiki\/Test-driven_development#:~:text=Test%2Ddriven%20development%20(TDD),so%20that%20the%20tests%20pass.\" rel=\"nofollow noopener\" target=\"_blank\">Test Driven Development<\/a> (TDD).<\/li>\n\n\n\n<li>It is SEO (Search Engine Optimization) friendly.<\/li>\n\n\n\n<li>All the objects and classes are independent of each other hence they can be tested separately.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Disadvantages of MVC Architecture:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The framework navigation becomes complex when new layers are added.<\/li>\n\n\n\n<li>Knowledge of multiple technologies is required. A developer should be well skilled in multiple technologies.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MVC (Model View Controller) is a design pattern that separates business logic, presentation logic, and data. MVC is used to design both web applications as well as mobile applications. It consists of three layers: Model Layer: Example: View Layer: Example: Controller Layer: Example: Advantages of MVC Architecture: Disadvantages of MVC Architecture:<\/p>\n","protected":false},"author":1,"featured_media":3644,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[931,935,930,926,933,928,1827,927,1828,934,929],"class_list":["post-3632","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-advantages-of-mvc","tag-controller","tag-controller-layer","tag-disadvantages","tag-model","tag-model-layer","tag-model-view-controller","tag-mvc-architecture","tag-test-driven-development","tag-view","tag-view-layer"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3632"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3632\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3644"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}