MVC(Model、View、Control)设计模式
一.MVC设计思想
现在的软件体系架构一般都是前后端进行分离操作的,让后台的逻辑隐蔽,前端的显示分离,让程序的耦合性变小,这样就算项目中途临时换了程序员,也能够很快的理解程序并推进项目。
注: 接下来为了方便理解类之间的设计关系,采用StartUML画类图,用C++编程
二.MVC编程实例
1.学生数据库MVC设计
类图设计
:
程序如下
:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
/*
MVC模式:(Model-View-Control模式)
对于MVC模式,View与Control属于关联关系,Model与Control也属于关联关系
MVC模式有效果的将前后端进行了分离,并且用Control模式进行控制。
*/
//Model模型类
class StudentModel
{
string _name;
int _age;
public:
string getName() { return _name; }
void setName(string name) { _name = name; }
int getAge() { return _age; }
void setAge(int age) { _age = age; }
};
//View视图类
class StudentView
{
public:
void printStudentDetails(StudentModel& model)
{
cout << model.getName() << ":" << model.getAge() << endl;
}
};
//Control控制类
class StudentControl
{
StudentModel& _model;
StudentView& _view;
public:
StudentControl(StudentModel& model, StudentView& view)
:_model(model)
,_view(view)
{
}
void setStudentName(string name)
{
_model.setName(name);
}
void setStudentAge(int age)
{
_model.setAge(age);
}
string getStudentName()
{
return _model.getName();
}
int getStudentAge()
{
return _model.getAge();
}
void updateView()
{
_view.printStudentDetails(_model);
}
};
//模拟数据库返回程序
static StudentModel retrieveFromDatabase()
{
StudentModel model;
model.setName("Worthy");
model.setAge(23);
return model;
}
int main()
{
//假设这是从数据库返回的数据
StudentModel model = retrieveFromDatabase();
StudentView view;
StudentControl control(model, view);
control.updateView();
control.setStudentAge(33);
control.updateView();
control.setStudentName("Wwx");
control.updateView();
return 0;
}
2.练习:通过类图写出程序
类图设计
:
程序
:
#include <iostream>
#include <string>
#include <map>
#include <memory>
using std::cout;
using std::endl;
using std::map;
using std::string;
/*
MVC模式练习:根据类图编写出代码
*/
/************************数据层 Student类**************************/
class Student
{
size_t _id;
string _name;
public:
Student(size_t id, string name)
: _id(id), _name(name)
{
}
size_t getId() const { return _id; }
string getName() const { return _name; }
};
/************************显示层 View类**************************/
class View
{
public:
virtual void show(map<size_t, Student> &_students) const
{
for (auto &_student : _students)
cout << _student.first << ":" << _student.second.getName() << endl;
}
};
/************************逻辑控制层 Control类**************************/
class Control
{
map<size_t, Student> _students;
public:
void attach(const Student &stu)
{
_students.insert(std::make_pair(stu.getId(), stu));
}
void detach(const Student& stu)
{
_students.erase(stu.getId());
}
void show(View *view)
{
view->show(_students);
}
};
/************************假设想在视图View层转换输出方式**************************/
class View2 : public View
{
public:
void show(map<size_t, Student> &_students) const
{
for (auto &_student : _students)
cout << "[" << _student.first << "->" << _student.second.getName() << "]" << endl;
}
};
/************************Main函数**************************/
int main()
{
Student s1(1, "Worthy");
Student s2(2, "gorge");
std::unique_ptr<Control> control(new Control());
control->attach(s1);
control->attach(s2);
std::unique_ptr<View> view2(new View2());
control->show(view2.get());
control->detach(s2);
std::unique_ptr<View> view(new View());
control->show(view.get());
return 0;
}