Simple understanding of mvc design pattern

introduction

MVC design pattern is to separate the data model from the view, establish the relationship between the data model and the view through the controller, and automatically update the view when the data model changes. Among them, M-data model, V-view and C-controller. The following is a simple use of MVC design pattern.

Examples

The development environment is qtcreater6 0.2. Create a console output program and set the running environment of the project to run in terminal The terminal can be started when the program is started, and the operation results of the corresponding program can be viewed at the terminal. The following is the implementation code.
main.cpp

#include <QCoreApplication>
#include <iostream>
#include <QDebug>
#include "mvcstudent.h"
//using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //Design pattern MVC
    MVCstudent student;

    return a.exec();
}

studentmodel.h

#ifndef STUDENTMODEL_H
#define STUDENTMODEL_H
#include <QString>


class StudentModel
{
public:
    StudentModel();
    void setStudentName(QString name);
    void setStudentNumber(int num);

    QString getStudentName();
    int getStudentNum();
private:
    QString m_strName;
    int m_nNumber;
};

#endif // STUDENTMODEL_H

studentmodel.cpp

#include "studentmodel.h"

StudentModel::StudentModel()
{

}

void StudentModel::setStudentName(QString name)
{
    m_strName = name;
}

void StudentModel::setStudentNumber(int num)
{
    m_nNumber = num;
}

QString StudentModel::getStudentName()
{
    return m_strName;
}

int StudentModel::getStudentNum()
{
    return m_nNumber;
}

studentview.h

#ifndef STUDENTVIEW_H
#define STUDENTVIEW_H
#include <QString>

class StudentView
{
public:
    StudentView();
    void outputStudentInfo(QString name,int studentId);
};

#endif // STUDENTVIEW_H

studentview.cpp

#include "studentview.h"

StudentView::StudentView()
{

}

void StudentView::outputStudentInfo(QString name,int studentId)
{
    qDebug("Student information: Name:%s|Student number:%d",qPrintable(name),studentId);
}

studentcontroller.h

#ifndef STUDENTCONTROLLER_H
#define STUDENTCONTROLLER_H
#include "studentmodel.h"
#include "studentview.h"
#include <QString>


class StudentController
{
public:
    StudentController();
    StudentController(StudentModel model,StudentView view);

    void setStudentName(QString name);
    void setStudentId(int num);
    QString getStudentName();
    int getStudentId();
    void updateView();
private:
    StudentModel m_studentModel;
    StudentView m_studentView;
};

inline void StudentController::setStudentId(int num)
{
    m_studentModel.setStudentNumber(num);
}

#endif // STUDENTCONTROLLER_H

studentcontroller.cpp

#include "studentcontroller.h"

StudentController::StudentController()
{

}

StudentController::StudentController(StudentModel model, StudentView view)
{
    m_studentModel = model;
    m_studentView = view;
}

void StudentController::setStudentName(QString name)
{
    m_studentModel.setStudentName(name);
}

QString StudentController::getStudentName()
{
    return m_studentModel.getStudentName();
}

int StudentController::getStudentId()
{
    return m_studentModel.getStudentNum();
}

void StudentController::updateView()
{
    m_studentView.outputStudentInfo(m_studentModel.getStudentName(),m_studentModel.getStudentNum());
}

mvcstudent.h

#ifndef MVCSTUDENT_H
#define MVCSTUDENT_H
#include "studentmodel.h"
#include "studentview.h"
#include "studentcontroller.h"

class MVCstudent
{
public:
    MVCstudent();
    StudentModel getStudentData();
};

#endif // MVCSTUDENT_H

mvcstudent.cpp

#include "mvcstudent.h"

MVCstudent::MVCstudent()
{
    StudentModel model = getStudentData();
    StudentView view;
    StudentController controller(model,view);
    controller.updateView();
    controller.setStudentName("timely");
    controller.updateView();
}

StudentModel MVCstudent::getStudentData()
{
    StudentModel model;
    model.setStudentName("Nurse telephone");
    model.setStudentNumber(12412);
    return model;
}

The above is a simple implementation of mvc design pattern.

Project structure

The structure of the project is shown in the figure below;

Operation results

Since the project is a console output application, the setting program can Run on the terminal. Setting method: click the project in the left column, and then the window shown in the figure below will be displayed. Select Run under build & Run, and then check run in terminal in the running setting.

The operation results are as follows:

understand

This article is a preliminary exploration of MVC design pattern. Its purpose is to separate the view from the data model and establish the connection between the data model and the view through the controller, that is, encapsulate the code related to interface display in the view class, encapsulate the data for display in the data model, and establish the connection between the data model and the view through the controller class, When the data model changes, the view is updated, which requires the data model and view object in the controller class as data members. The data model can be operated through the public interface function provided by the controller. Therefore, the setting function is provided. The setting function of the data model class is called inside the setting function to change the value of member variables in the data model class, In addition, you can update the display of the interface through the update function in the controller class, that is, update the view, and call the interface display function in the view class through the member variable view class object of the controller. Finally, we need to define the data model class object, view object and controller object, change the data model through the controller, and then update the view display.
The above is my preliminary understanding.

Keywords: C++ Qt mvc

Added by chetanrakesh on Wed, 16 Feb 2022 05:37:39 +0200