赞
踩
MVC模型是(model模型 view视图 controller控制器)是一种软件设计的框架模式,它采用model-view-controller的方法把业务逻辑,数据与视图显示分离,把众多的业务逻辑集合在一个部件里。当然这样并不能让我们理解它,简单的说,就是一种把数据模型,视图显示与人机交互三者分离开的一种编程方式。
为什么要引入MVC模型呢?
(1)可以使同一个程序使用不同的表现形式,如果控制器反馈给模型的数据发生了变化,那么模型将及时通知有关的视图,视图会对应的刷新自己所展现的内容
(2)因为模型是独立于视图的,所以模型可复用,模型可以独立的移植到别的地方继续使用
(3)前后端的代码分离,使项目开发的分工更加明确,程序的测试更加简便,提高开发效率
其实控制器的功能类似于一个中转站,会决定调用那个模型去处理用户请求以及调用哪个视图去呈现给用户
在实现这个程序之前,有一点需要先给大家提前讲解一下。因为我们已经知道,这里面最少要自定义三个头文件,那么这三个头文件该如何互相去引用呢?
示例:
- /A.h
- #ifndef A_H
- #define A_H
- class A{
- B* b;
- };
- #endif
-
- A.cpp
- #include"A.h"
- *********
- *********
-
- B.h
- #ifndef B_H
- #define B_H
- class B{
- A* a;
- };
- #endif
-
- /B.cpp
- #include"B.h"
- *********
- *********
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
假设现在有两个头文件需要相互引用,我们该如何去引用呢?
错误示范:
- /A.h
- #ifndef A_H
- #define A_H
- #include"B.h"
- class A{
- B* b;
- };
- #endif
-
-
- A.cpp
- #include"A.h"
- *********
- *********
-
- B.h
- #ifndef B_H
- #define B_H
- #include"A.h"
- class B{
- A* a;
- };
- #endif
-
- /B.cpp
- #include"B.h"
- *********
- *********
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
正确示范:
- /A.h
- #ifndef A_H
- #define A_H
- class B;
- class A{
- B* b;
- };
- #endif
-
-
- A.cpp
- #include"A.h"
- #include"B.h"
- *********
- *********
-
- B.h
- #ifndef B_H
- #define B_H
- class A;
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。