-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstudent_model_view_controller.cpp
executable file
·137 lines (104 loc) · 2.84 KB
/
student_model_view_controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <string>
#include <iostream>
/*
https://www.tomdalling.com/blog/software-design/model-view-controller-explained/
https://www.tutorialspoint.com/design_pattern/mvc_pattern.htm
Model:
Model represents an object carrying data and does nothing else.
The model does NOT depend on the controller or the view.
View:
View represents the visualization of the data that model contains. The view displays the model data,
and sends user actions (e.g. button clicks) to the controller.
Controller:
Controller acts on both model and view. It controls the data flow into model object and updates the view
whenever data changes. It keeps view and model separate.
The model represents the data, and does nothing else. The model does NOT depend on the controller or the view.
MVC makes model classes and view classes reusable without modification.
*/
////////////////////////////////// Model //////////////////////////////////
class Student
{
private:
std::string rollNo;
std::string name;
public:
std::string getRollNo()
{
return rollNo;
}
void setRollNo(std::string rollNo)
{
this->rollNo = rollNo;
}
std::string getName()
{
return name;
}
void setName(std::string name)
{
this->name = name;
}
};
////////////////////////////////// View //////////////////////////////////
class StudentView
{
public:
void printStudentDetails(std::string studentName, std::string studentRollNo)
{
std::cout<<"Student: "<<std::endl;
std::cout<<"Name: " << studentName<<std::endl;
std::cout<<"Roll No: " << studentRollNo<<std::endl;
}
};
////////////////////////////////// Controller //////////////////////////////////
class StudentController
{
private:
Student model;
StudentView view;
public:
StudentController(Student model, StudentView view)
{
this->model = model;
this->view = view;
}
void setStudentName(std::string name)
{
model.setName(name);
}
std::string getStudentName()
{
return model.getName();
}
void setStudentRollNo(std::string rollNo)
{
model.setRollNo(rollNo);
}
std::string getStudentRollNo()
{
return model.getRollNo();
}
void updateView()
{
view.printStudentDetails(model.getName(), model.getRollNo());
}
};
Student retriveStudentFromDatabase()
{
Student student;
student.setName("Robert");
student.setRollNo("10");
return student;
}
int main()
{
//fetch student record based on his roll no from the database
Student model = retriveStudentFromDatabase();
//Create a view : to write student details on console
StudentView view;
StudentController controller(model, view);
controller.updateView();
//update model data
controller.setStudentName("John");
controller.updateView();
}