当前位置: 首页 > news >正文

MVC架构模式

MVC架构模式

  • 前言
  • MVC流程
  • demo
  • 补充

前言

为了更好地完成天气预报的仿写,这里先学习一下MVC。

MVC 是 Model-View-Controller 的缩写,是一种经典的软件架构模式(针对系统结构和模块职责分配的一种通用解决方案)。

Model(模型):负责数据处理,包含仿写天气预报中的网络请求。

View(视图):负责UI界面,例如UIView或UITableViewCell。

Controller(控制器):View不直接修改Model,而是通过Controller连接,处理用户交互,例如UIViewController。

MVC流程

这里展示一下整个数据在MVC架构中的流动:

用户操作 → View → Controller → Model(处理数据,网络请求获取天气数据并存储)→ Controller → View(刷新UI)

demo

下面一个简单的天气预报MVC为例:

  • Model:网络请求天气API数据。
#import <Foundation/Foundation.h>@interface WeatherModel : NSObject@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *temperature;- (void)fetchWeatherForCity:(NSString *)city completion:(void(^)(WeatherModel *weather, NSError *error))completion;@end#import "WeatherModel.h"@implementation WeatherModel//请求完成后回调
- (void)fetchWeatherForCity:(NSString *)city completion:(void(^)(WeatherModel *weather, NSError *error))completion {NSString *urlString = [NSString stringWithFormat:@"https://api.example.com/weather?city=%@", city];NSURL *url = [NSURL URLWithString:urlString];[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {if (error) {completion(nil, error);return;}NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];WeatherModel *weather = [[WeatherModel alloc] init];weather.city = json[@"city"];weather.temperature = [NSString stringWithFormat:@"%@℃", json[@"temp"]];completion(weather, nil);}] resume];
}@end
  • View:UI界面绘制。

  • Controller:接收View的操作(点击按钮调用方法),检查输入,调用Model,发起网络请求,网络请求完成后,回调返回数据,再切回主线程更新UI。

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UITextField *cityTextField;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;- (IBAction)searchWeather:(id)sender;@end#import "ViewController.h"
#import "WeatherModel.h"@implementation ViewController//IBAction是一个和 Storyboard 上 UI 控件(比如按钮)绑定的事件方法
- (IBAction)searchWeather:(id)sender {NSString *city = self.cityTextField.text;if (city.length == 0) {self.resultLabel.text = @"请输入城市名称";return;}WeatherModel *model = [[WeatherModel alloc] init];//block回调[model fetchWeatherForCity:city completion:^(WeatherModel *weather, NSError *error) {//UI更新必须在主线程//切回主线程dispatch_async(dispatch_get_main_queue(), ^{if (!self) {return;}if (error) {self.resultLabel.text = @"获取失败";} else {self.resultLabel.text = [NSString stringWithFormat:@"%@:%@", weather.city, weather.temperature];}});}];
}
@end

这里再展示一下使用MVC架构模式完成注册登录功能的文件:

请添加图片描述

补充

这里再认识几个常见的架构模式:

  • 分层模式:把系统划分成多个层次,每一层有明确的职责,只和“相邻层”通信。通常是“三层架构”:表示层、业务逻辑层、数据访问层。
  • 客户端-服务器模式:是一种 分布式系统架构,将系统分为两类角色:客户端(负责与用户交互)、服务器(等待并处理来自客户端的请求)。
  • 代理模式:为某个对象提供一个代理对象,由代理对象来控制对原始对象的访问。比如:通过URLSession封装远程 API 的代理类。
http://www.dtcms.com/a/360313.html

相关文章:

  • XXL-JOB任务执行The access token is wrong问题分析解决及原理源码解析
  • 【Linux】linux进程 vs 线程
  • 《WINDOWS 环境下32位汇编语言程序设计》第9章 通用控件(2)
  • Modbus CRC16校验码在线计算器
  • Python训练营打卡Day49-神经网络调参指南
  • 大模型参数量与计算量(FLOPs)估算方法
  • [WUSTCTF2020]B@se1
  • 后向投影合成孔径辐射源定位方法(一)
  • Linux-数据库
  • MVC模式学习
  • 物种多样性与物种丰富度
  • 制造业生产线连贯性动作识别系统开发
  • 使用 Claude Code 与 Remotion 制作自定义动画视频的完整教程
  • 代码分析之符号执行技术
  • 多人协作开发指南二
  • 简化对齐训练:用明文对比数据SFT替代复杂DPO
  • 8针脚的1.8寸IIC接口的TFT彩屏的八个引脚都需要使用吗?
  • 【编号186】中国劳动统计年鉴(1991-2023)
  • LeetCode 2570.合并两个二维数组
  • 超越关键词:RAG系统如何破解用户查询的“模糊密码”
  • BLE广播与扫描
  • 嵌入式C学习笔记之预编译
  • Redis面试重点-2
  • Coze源码分析-工作空间-项目开发-前端源码
  • 在Windows系统Docker中使用wsl2、容器、windows文件路径三种不同挂载方式的区别和性能差异
  • ceph对象存储-存储池-用户认证
  • @Value注解的底层原理(一)
  • Day18 (前端:JavaScript基础阶段)
  • 数据结构 04(线性:双向链表)
  • Ansible 临时命令与常用模块实操指南