Qt project architecture: MVC architecture

The MVC pattern stands for the Model-View-Controller (Model-View-Controller) pattern. This pattern is used for layered development of applications.

  • Model - is the part of the application that handles the application data logic. Usually model objects are responsible for accessing data in the database.
  • View (view) - is the part of the application that handles the display of data. Usually views are created from model data.
  • Controller (controller) - is the part of the application that handles user interaction. Typically the controller is responsible for reading data from the view, controlling user input, and sending data to the model.

MVC layering helps manage complex applications because you can focus on one aspect at a time. For example, you can focus on view design without relying on business logic. It also makes testing the application easier.

MVC layering also simplifies group development. Different developers can develop views, controller logic, and business logic at the same time.

Take the C++ code as an example to design a hero (Hero) battle demo. Hero1 attacks Hero2, Hero2 loses blood continuously, and the UI is updated synchronously. The main three classes are Hero (hero data class), HeroView (hero UI class, such as blood bar) and HeroController (hero controller class, Hero management class).

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

The Hero class:

#pragma once 
 
class Hero
{
public:
    Hero();
    ~Hero();
    //be attacked
    void beAttack(int att);
    //is dead?
    bool dead() { return m_nHp == 0; }
    public:
    //set no
    void setNo(std::string no) { m_sNo = no; }
    //get no
    std::string getNo() { return m_sNo; }

    //set name
    void setName(std::string name) { m_sName = name; }
    //get name
    std::string getName() { return m_sName; }

    //get hp
    void setHp(int hp) { m_nHp = hp; }
    //set hp
    int getHp() { return m_nHp; }

    //set att
    void setAtt(int att) { m_nAtt = att; }
    //get att
    int getAtt() { return m_nAtt; }
private:

    std::string m_sName;//姓名
    std::string m_sNo;//游戏场号码

    int m_nHp;//血量
    int m_nAtt;//攻击力
};
#include "stdafx.h"
#include "Hero.h"

//ctor
Hero::Hero()
{
}

//Destructor
Hero::~Hero()
{
 
}
 
void Hero::beAttack(int att)
{
    if (att <= 0) throw "Att Number <= 0";//safe check
    m_nHp -= att;
    m_nHp = m_nHp < 0 ? 0 : m_nHp;//safe check
}

The HeroView class:

#pragma once
class HeroView
{
public:
    HeroView();
    ~HeroView();

    //show hero UI data;
    void show(string no, string name,int att, int hp);
    //show hero dead UI;
    void dead(string no, string name);
    //show heor winned UI;
    void winned(string no, string name);
    private:
    //Hero* m_hero;
};
#include "stdafx.h"
#include "HeroView.h"

HeroView::HeroView()
{
}

HeroView::~HeroView()
{
}

void HeroView::show(string no,string name, int att, int hp)
{
    cout << "Hero info:"<<"(no:"<<no<<",name:"<<name<<",att:"<<att<<",hp:"<<hp<<")" << endl;
}

void HeroView::dead(string no, string name)
{
    cout << "Hero Dead:" << "(no:" << no << ",name:"<<name << ")" << endl;
}

void HeroView::winned(string no, string name)
{
    cout << "Hero Winned:" << "(no:" << no << ",name:" << name << ")" << endl;

}

The HeroController class:

#pragma once
class HeroController
{
public:
      HeroController(string no, string name);
      ~HeroController();
      void setHeroHp(int hp);//set hero hp
      void setHeroAtt(int att);//set hero att
      void show();//show hero info
      void beAttack(int att);//be attacked by hero
      void dead();//dead
      void winned();//winned

     Hero* getHero() { return m_hero; }//get hero
 
private:
     Hero * m_hero;//hero
     HeroView * m_heroView;//hero view
};
#include "stdafx.h"
#include "HeroController.h"
 
HeroController::HeroController(string no, string name)
{
    m_heroView = new HeroView();
    m_hero = new Hero();
    m_hero->setNo(no);
    m_hero->setName(name);
}

HeroController::~HeroController()
{
}

void HeroController::setHeroHp(int hp)
{
    m_hero->setHp(hp);
}

void HeroController::setHeroAtt(int att)
{
    m_hero->setAtt(att);
}

void HeroController::show()
{
    m_heroView->show(m_hero->getNo(), m_hero->getName(),m_hero->getAtt(), m_hero->getHp());
}

void HeroController::beAttack(int att)
{
    m_hero->beAttack(att);
}

void HeroController::dead()
{
    m_heroView->dead(m_hero->getNo(),m_hero->getName());
}

void HeroController::winned()
{
    m_heroView->winned(m_hero->getNo(), m_hero->getName());
}

main() tests:

// ConsoleApplication_C++1.cpp: 主项目文件。
  
#include "stdafx.h"

using namespace System;

int main()
{
    //初始化一个英雄的控制器
    HeroController* controller = new HeroController("2017","孟栋");
    //设置血量100
    controller->setHeroHp(100);
    controller->setHeroAtt(40);
    //显示一下血量
    controller->show();

    //初始化第二个英雄的控制器
    HeroController* controller2 = new HeroController("2016", "黑魔王");
    //设置血量100
    controller2->setHeroHp(200);
    //设置攻击力20
    controller2->setHeroAtt(20);
    //显示一下血量
    controller2->show();

    //hero1 attack hero2
    controller2->beAttack(controller->getHero()->getAtt());
    //ui更新
    controller2->show();

    //如果没有dead,就一直攻击
    while (!controller2->getHero()->dead())
    {
        //hero1 attack hero2
        controller2->beAttack(controller->getHero()->getAtt());
        //ui更新
        controller2->show();
    }

    controller2->dead();
    controller->show();
    controller->winned();

    return 0;
}

The console output is as follows:

Hero info : (no:2017, name : 孟栋, att : 40, hp : 100)
Hero info : (no : 2016, name : 黑魔王, att : 20, hp : 200)
Hero info : (no : 2016, name : 黑魔王, att : 20, hp : 160)
Hero info : (no : 2016, name : 黑魔王, att : 20, hp : 120)
Hero info : (no : 2016, name : 黑魔王, att : 20, hp : 80)
Hero info : (no : 2016, name : 黑魔王, att : 20, hp : 40)
Hero info : (no : 2016, name : 黑魔王, att : 20, hp : 0)
Hero Dead : (no : 2016, name : 黑魔王)
Hero info : (no : 2017, name : 孟栋, att : 40, hp : 100)
Hero Winned : (no : 2017, name : 孟栋)

 The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/QtCompany/article/details/131584355