The Permanent URL is: Model-View-Controller Explained in C++.

The Model-View-Controller (MVC) is not a technology, but a concept in software design/engineering. The MVC consists of three components, the Model, the View and the Controller, as illustrated in below figure.

model-view-controller-mvc-explained

THE MODEL

The Model is directly responsive for handling data. For example, the Model component accesses MySQL database. The Model should not rely on other components such as View or Controller. In other words, the Model does not care how its data can be displayed or when to be updated.

The data changes in the Model will generally be published through some event handlers. For example, the View model must register on the Model so that it understands the data changes. We can define a function callback when data changes:

1
2
3
4
5
6
// common.h
// https://helloacm.com/model-view-controller-explained-in-c/
#pragma once
#include <string>
using namespace std;
typedef void (*DataChangeHandler)(string newData);

DataChangeHandler is now a function pointer type that returns void and takes a parameter of a string (data type). The Model is responsible for data retrieval and optionally, it can register the data-change-event.

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
// model.h
// https://helloacm.com/model-view-controller-explained-in-c/
#pragma once
#include <string>
using namespace std;
#include "common.h"
// Model is responsible for data get and set
class Model {
    public:
        Model(const string &data) {
            this->SetData(data);
        }
        Model() { } // default constructor
        string Data(){
            return this->data;
        }
 
        void SetData(const string &data) {
            this->data = data;
            if (this->event != nullptr) { // data change callback event
                this->event(data);
            }  
        }
        //  register the event when data changes.
        void RegisterDataChangeHandler(DataChangeHandler handler) {
            this->event = handler;
        }
    private:
        string data = "";
        DataChangeHandler event = nullptr;
};

VIEW

The View component knows how to present the Data to the users. It needs to access the Model and normally needs to define its ‘Render()’ function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// view.h
// https://helloacm.com/model-view-controller-explained-in-c/
#pragma once
#include <iostream>                  
#include "model.h"                                              
// View is responsible to present data to users
class View {
    public:
        View(const Model &model) {
            this->model = model;
        }
        View() {}
        void SetModel(const Model &model) {
            this->model = model;
        }
        void Render() {
            std::cout << "Model Data = " << model.Data() << endl;
        }
    private:
        Model model;
};

CONTROLLER

The Controller can ask the Model to update its data. Also, the Controller can ask the View to change its presentation, e.g. Showing a Dialog instead of Outputing to Console. Basically it is a component that takes input from the user and sends commands to the View or Model.

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
// controller.h
// https://helloacm.com/model-view-controller-explained-in-c/
#pragma once
#include "model.h"
#include "view.h"
 
// Controller combines Model and View
class Controller {
    public:
        Controller(const Model &model, const View &view) {
          this->SetModel(model);
          this->SetView(view);        
        }
        void SetModel(const Model &model) {
            this->model = model;
        }
        void SetView(const View &view) {
            this->view = view;
        }
        // when application starts
        void OnLoad() {
            this->view.Render();
        }
    private:
        Model model;
        View view;
};

MVC DEMO

With the above three component classes, we can have the following code to demonstrate MVC.

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
// mvc.cpp
// https://helloacm.com/model-view-controller-explained-in-c/
#include <iostream>
#include "view.h"
#include "model.h"
#include "controller.h"
#include "common.h"
 
using namespace std;
void DataChange(string data) {
  cout << "Data Changes: " << data <<endl;
}
 
int main() {
    Model model("Model");
    View view(model);    
    // register the data-change event
    model.RegisterDataChangeHandler(&DataChange);
    // binds model and view.
    Controller controller(model, view);
    // when application starts or button is clicked or form is shown...
    controller.OnLoad();
    model.SetData("Changes"); // this should trigger View to render
    return 0;
}

To avoid the circular dependency in C++ between class View and Model, we use a function pointer to represent the event of data-change instead of the pointer to a member of object. To compile the above code, use the following command:

1
 g++ --std=c++11 mvc.cpp

Then run ./a.out should give you:

1
2
Model Data = Model
Data Changes: Changes

The model.SetData(“Changes”); triggers the data-change event that is registered in the Model component.

https://helloacm.com/model-view-controller-explained-in-c/

Model-View-Controller Explained in C++的更多相关文章

  1. MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

    MVC模式(Model View Controller): Model:DAO模型 View:JSP  在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若 ...

  2. MVC(Model View Controller)框架

    MVC框架 同义词 MVC一般指MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...

  3. 深入浅出Java MVC(Model View Controller) ---- (JSP + servlet + javabean实例)

    在DRP中终于接触到了MVC,感触是确实这样的架构系统灵活性不少,现在感触最深的就是使用tomcat作为服务器发布比IIS好多了,起码发布很简单,使用起来方便. 首先来简单的学习一下MVC的基础知识, ...

  4. Model View Controller (MVC) Overview

    By Rakesh Chavda on Jul 01, 2015 What is MVC?Model View Controller is a type of user interface archi ...

  5. Model View Controller(MVC) in PHP

    The model view controller pattern is the most used pattern for today’s world web applications. It ha ...

  6. What is the difference between Reactjs and Rxjs?--React is the V (View) in MVC (Model/View/Controller).

    This is really different, React is view library; and Rxjs is reactive programming library for javasc ...

  7. Model View Controller

    On the iPhone or iPod touch, a modal view controller takes over the entire screen. This is the defau ...

  8. 设计模式 --- 模型-视图-控制器(Model View Controller)

    模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程 ...

  9. MVC4 Model View Controller分离成独立项目

    适合人群:了解MVC项目的程序员 开发工具:vs2012 开发语言:C# 小项目或功能比较单一的项目可以直接新建一个MVC基本项目类型即可,但随着需求不断迭代,项目的功能模块越来越多,甚至有些模块可以 ...

  10. Qt Model/View(官方翻译,图文并茂)

    http://doc.trolltech.com/main-snapshot/model-view-programming.html 介绍 Qt 4推出了一组新的item view类,它们使用mode ...

随机推荐

  1. WPF入门(三)->几何图形之椭圆形(EllipseGeometry)

    原文:WPF入门(三)->几何图形之椭圆形(EllipseGeometry) 我们可以使用EllipseGeometry 来绘制一个椭圆或者圆形的图形 EllipseGeometry类: 表示圆 ...

  2. [GeekBand] STL Traits 使用简介

    本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Templates  15章节 :网络资料: http://blog.csdn.net/my_business/article/d ...

  3. com.sun.mirror的jar包

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sinat_36246371/article/details/53170166 <Java編程思 ...

  4. CAP和最终一致性

    查阅资料整理了最终一致性.CAP 相关的内容.由于图省事儿,没有做文字的整理记载,只有 slides 和一些查阅过的链接,大家将就着看.欢迎指正. slides: slides 链接:请戳这里 背景 ...

  5. 我是怎么做App token认证的

    使用Token来做身份认证在目前的移动客户端上非常流行,Token这个概念来源于OAuth认证,主要是在服务端实现.关于相关的原理,同学们自行百度.在这里,我简单介绍一下我是怎么具体实现的,重点描述t ...

  6. c语言学习笔记(7)——数组

    一.为什么需要数组1.为了解决大量同类型的数据存储和使用2.为了模拟现实世界二.数组的分类1.一维数组为n个变量连续分配存储空间所有的变量数据类型必须相同所有变量所占的字节大小必须相等初始化:完全初始 ...

  7. xshell登陆Win10 Linux子系统

    原文:xshell登陆Win10 Linux子系统 版权声明:转载请注明出处 https://blog.csdn.net/anychenp/article/details/78922320 修改端口 ...

  8. WPF中使用AxisAngleRotation3D实现CAD的2D旋转功能

    原文:WPF中使用AxisAngleRotation3D实现CAD的2D旋转功能       对于CAD图形来说,3D旋转比较常用,具体实现方法在上篇文章<WPF中3D旋转的实现 >中做了 ...

  9. kendo ui gird温馨提示(使用本地数据) 一个

    加入js引用 <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.common.min.css" r ...

  10. 微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传

    1.index.wxml <!--index.wxml--> <button style="margin:30rpx;" bindtap="choose ...