MVC(Model、View、Control)設計模式

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章