文章目录
C语言 MVC模式详解与实践 - 温度监控系统
1. 什么是MVC模式?
MVC模式将应用程序分为三个核心部分:模型(Model)、视图(View)和控制器(Controller)。这种模式有助于实现关注点分离,使得程序更容易维护和扩展。
2. 为什么需要MVC模式?
- 关注点分离
- 代码复用性高
- 并行开发
- 便于维护
- 利于测试
3. 实际应用场景
- 温度监控系统
- 数据采集显示
- 设备状态监控
- 参数配置界面
- 系统控制面板
4. 代码实现
4.1 UML 关系图
4.2 头文件 (temp_model.h)
#ifndef TEMP_MODEL_H
#define TEMP_MODEL_H
typedef struct {
float temperature;
float threshold;
int alarm_status;
} TempModel;
TempModel* create_model(void);
void destroy_model(TempModel* model);
void update_temperature(TempModel* model, float temp);
int check_alarm(TempModel* model);
#endif // TEMP_MODEL_H
4.3 头文件 (temp_view.h)
#ifndef TEMP_VIEW_H
#define TEMP_VIEW_H
#include "temp_model.h"
typedef struct {
void (*display_temperature)(float temp);
void (*display_alarm)(int status);
} TempView;
TempView* create_view(void);
void destroy_view(TempView* view);
#endif // TEMP_VIEW_H
4.4 头文件 (temp_controller.h)
#ifndef TEMP_CONTROLLER_H
#define TEMP_CONTROLLER_H
#include "temp_model.h"
#include "temp_view.h"
typedef struct {
TempModel* model;
TempView* view;
void (*process_input)(struct TempController* self, float temp);
} TempController;
TempController* create_controller(TempModel* model, TempView* view);
void destroy_controller(TempController* controller);
#endif // TEMP_CONTROLLER_H
4.5 实现文件 (temp_model.c)
#include "temp_model.h"
#include <stdlib.h>
TempModel* create_model(void) {
TempModel* model = malloc(sizeof(TempModel));
model->temperature = 0.0f;
model->threshold = 30.0f;
model->alarm_status = 0;
return model;
}
void destroy_model(TempModel* model) {
free(model);
}
void update_temperature(TempModel* model, float temp) {
model->temperature = temp;
model->alarm_status = (temp > model->threshold);
}
int check_alarm(TempModel* model) {
return model->alarm_status;
}
4.6 实现文件 (temp_view.c)
#include "temp_view.h"
#include <stdio.h>
#include <stdlib.h>
static void display_temperature(float temp) {
printf("当前温度: %.1f°C\n", temp);
}
static void display_alarm(int status) {
if (status) {
printf("警告: 温度过高!\n");
}
}
TempView* create_view(void) {
TempView* view = malloc(sizeof(TempView));
view->display_temperature = display_temperature;
view->display_alarm = display_alarm;
return view;
}
void destroy_view(TempView* view) {
free(view);
}
4.7 实现文件 (temp_controller.c)
#include "temp_controller.h"
#include <stdlib.h>
static void process_input(TempController* self, float temp) {
// 更新模型
update_temperature(self->model, temp);
// 更新视图
self->view->display_temperature(temp);
// 检查并显示警报
if (check_alarm(self->model)) {
self->view->display_alarm(1);
}
}
TempController* create_controller(TempModel* model, TempView* view) {
TempController* controller = malloc(sizeof(TempController));
controller->model = model;
controller->view = view;
controller->process_input = process_input;
return controller;
}
void destroy_controller(TempController* controller) {
free(controller);
}
4.8 主程序 (main.c)
#include "temp_controller.h"
#include <stdio.h>
#include <windows.h>
int main() {
// 创建MVC组件
TempModel* model = create_model();
TempView* view = create_view();
TempController* controller = create_controller(model, view);
// 模拟温度数据
float test_temps[] = {25.0f, 28.5f, 32.0f, 29.5f, 26.0f};
printf("=== 温度监控系统启动 ===\n\n");
for (int i = 0; i < 5; i++) {
printf("时间: %d秒\n", i + 1);
controller->process_input(controller, test_temps[i]);
printf("\n");
Sleep(1000); // 等待1秒
}
// 清理资源
destroy_controller(controller);
destroy_view(view);
destroy_model(model);
return 0;
}
5. 代码分析
5.1 关键设计点
- 模块职责分明
- 数据流向清晰
- 接口设计合理
- 资源管理完善
5.2 实现特点
- 低耦合高内聚
- 可扩展性好
- 代码复用性高
- 维护性好
6. 编译和运行
gcc -c temp_model.c temp_view.c temp_controller.c main.c
gcc temp_model.o temp_view.o temp_controller.o main.o -o temp_system
7. 注意事项
- 模块间依赖关系
- 内存管理
- 错误处理
- 资源释放
8. 改进建议
- 添加数据持久化
- 实现多视图支持
- 添加配置管理
- 优化性能
9. 总结
MVC模式通过分离关注点,使得温度监控系统的代码结构更加清晰,便于维护和扩展。这种模式特别适合处理数据、显示和用户交互的系统。
参考资料
- 《设计模式》
- 《C语言程序设计》
- 《嵌入式系统设计》
1170

被折叠的 条评论
为什么被折叠?



