Model-View-Controller Explained in C++
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 |
// common.h |
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 |
// model.h |
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 |
// view.h |
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 |
// controller.h |
MVC DEMO
With the above three component classes, we can have the following code to demonstrate MVC.
1 |
// mvc.cpp |
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 |
Model Data = Model |
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/
最新文章
- CSS样式表继承详解
- 查看某个线程占得CPU高
- Spring - 基于注解的组件扫描
- JavaWeb学习总结(十二)——Session
- Quarzt.NET 任务调度框架
- linux截图工具scrot
- 修改hosts文件(判断是否为管理员/以管理员权限运行脚本)
- 编程中的 if ()else() 语句
- Android 命令
- 处理FTP上传成功推理
- iter迭代器的应用
- 如何开发自己的搜索帝国之ES图形化Kibana安装与使用
- angular学习笔记04 理论加实践
- struts2_HelloWorld
- Javaweb学习笔记——(二十)——————Javaweb监听器、国际化
- maven创建helloword项目
- ajax实战用法详解
- [others] 一个酷酷的站
- The value for the useBean class attribute xxx is invalid
- 笔记-Python中逗号的作用
热门文章
- 【28.57%】【codeforces 615C】 Running Track
- 利用WPF建立自己的3d gis软件(非axhost方式)(十三)万能的用户层接口,(强大的WPF)
- React为啥很多类里的标签上事件处理函数要用bind(this)
- Himi浅谈游戏开发de自学历程!(仅供参考)
- numpy 下的数据结构与数据类型的转换(np.array vs. np.asarray)
- C#连接Oracle数据库乱码问题
- Opencv 张正友相机标定傻瓜教程
- Java编程思想学习笔记-使用显式的Lock对象
- Win10忘记ubuntu子系统密码
- 《DELPHI赋》