MVC with Update Example

This program demonstrates MVC update flow.

Problem Statement

Write a Java program where Controller updates Model and View reflects changes.

Source Code

1 class Model {
2 String data;
3 void setData(String d){ data=d; }
4 String getData(){ return data; }
5 }
6 class View {
7 void display(String data){ System.out.println("View: "+data); }
8 }
9 class Controller {
10 Model m; View v;
11 Controller(Model m,View v){ this.m=m; this.v=v; }
12 void setData(String d){ m.setData(d); }
13 void updateView(){ v.display(m.getData()); }
14 }
15 public class MVCUpdate {
16 public static void main(String[] args) {
17 System.out.println("Eduinq MVC Update");
18 Model m=new Model();
19 View v=new View();
20 Controller c=new Controller(m,v);
21 c.setData("Hello Eduinq MVC");
22 c.updateView();
23 }
24 }

Program Output

Eduinq MVC Update
View: Hello Eduinq MVC

Explanation

Controller updates Model. - View reflects updated data. - This demonstrates dynamic flow in MVC. Conclusion: Shows MVC update flow.

Quick Links to Explore