0% found this document useful (0 votes)
9 views

MVC Architecture

The document discusses how to implement the MVC architecture pattern in Java applications. It provides code examples of the model, view and controller components and how they interact together.

Uploaded by

Aarthi E
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

MVC Architecture

The document discusses how to implement the MVC architecture pattern in Java applications. It provides code examples of the model, view and controller components and how they interact together.

Uploaded by

Aarthi E
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MVC Architecture in Java: How to implement MVC in Java? Edureka https://www.edureka.

co/blog/mvc-architecture-in-java/

2 of 10 07-04-2024, 09:32 am
MVC Architecture in Java: How to implement MVC in Java? Edureka https://www.edureka.co/blog/mvc-architecture-in-java/

3 of 10 07-04-2024, 09:32 am
MVC Architecture in Java: How to implement MVC in Java? Edureka https://www.edureka.co/blog/mvc-architecture-in-java/

1 package MyPackage;
2
3 public class Course {
4 private String CourseName;
5 private String CourseId;
6 private String CourseCategory;
7
8 public String getId() {
9 return CourseId;
10 }
11
12 public void setId(String id) {
13 this.CourseId = id;
14 }
15
16 public String getName() {
17 return CourseName;
18 }
19
20 public void setName(String name) {
21 this.CourseName = name;
22 }
23
24 public String getCategory() {
25 return CourseCategory;
26 }
27
28 public void setCategory(String category) {
29 this.CourseCategory = category;
30 }
31
32 }

1 package MyPackage;
2
3 public class CourseView {
4 public void printCourseDetails(String CourseName, String CourseId, String CourseCategory){
5 System.out.println("Course Details: ");
6 System.out.println("Name: " + CourseName);
7 System.out.println("Course ID: " + CourseId);
8 System.out.println("Course Category: " + CourseCategory);
9 }
10 }

4 of 10 07-04-2024, 09:32 am
MVC Architecture in Java: How to implement MVC in Java? Edureka https://www.edureka.co/blog/mvc-architecture-in-java/

1 package MyPackage;
2
3 public class CourseController {
4 private Course model;
5 private CourseView view;
6
7 public CourseController(Course model, CourseView view){
8 this.model = model;
9 this.view = view;
10 }
11
12 public void setCourseName(String name){
13 model.setName(name);
14 }
15
16 public String getCourseName(){
17 return model.getName();
18 }
19
20 public void setCourseId(String id){
21 model.setId(id);
22 }
23
24 public String getCourseId(){
25 return model.getId();
26 }
27
28 public void setCourseCategory(String category){
29 model.setCategory(category);
30 }
31
32 public String getCourseCategory(){
33 return model.getCategory();
34 }
35 public void updateView(){
36 view.printCourseDetails(model.getName(), model.getId(), model.getCategory());
37 }
38 }

5 of 10 07-04-2024, 09:32 am
MVC Architecture in Java: How to implement MVC in Java? Edureka https://www.edureka.co/blog/mvc-architecture-in-java/

1 package MyPackage;
2
3 public class MVCPatternDemo {
4 public static void main(String[] args) {
5
6 //fetch student record based on his roll no from the database
7 Course model = retriveCourseFromDatabase();
8
9 //Create a view : to write course details on console
10 CourseView view = new CourseView();
11
12 CourseController controller = new CourseController(model, view);
13
14 controller.updateView();
15
16 //update model data
17 controller.setCourseName("Python");
18 System.out.println("nAfter updating, Course Details are as follows");
19
20 controller.updateView();
21 }
22
23 private static Course retriveCourseFromDatabase(){
24 Course course = new Course();
25 course.setName("Java");
26 course.setId("01");
27 course.setCategory("Programming");
28 return course;
29 }
30 }

1 Course Details:
2 Name: Java
3 Course ID: 01
4 Course Category: Programming
5
6 After updating, Course Details are as follows
7 Course Details:
8 Name: Python
9 Course ID: 01
10 Course Category: Programming

6 of 10 07-04-2024, 09:32 am

You might also like