用Protocol来分离的View和Model
来源:互联网 发布:mysql source用法 编辑:程序博客网 时间:2024/04/29 13:49
声明:
以下纯属个人见解,仅供参考. 有不足或错误的地方,请留言或Email至luffy243077002@163.com.
在项目中,不同的Model常常要显示在相同的View上. 例如tableView的cell,常常显示不同的Model.
做法1:
@interface LMNewBookListCell : UITableViewCell@property (nonatomic, strong) id dataSource;@end@implementation LMNewBookListCell- (void)setDataSource:(id)dataSource { _dataSource = dataSource; if ([dataSource isKindOfClass:[LMBookDetail class]]) { LMBookDetail *bookDetail = (LMBookDetail *)dataSource; self.titleLabel.text = bookDetail.name; //... }else if([dataSource isKindOfClass:[... class]]){ //... }}@end
如上,在view的.h文件设置传Model的属性,然后在.m文件中重写set方法.
在set方法中,判断是哪种类型的Model,对应进行装载Model.
缺点: View和Model耦合性比较强,而且随着Model类型越来越多,set方便if-else判断越来越长.
做法2:
@implementation LMRNoCoverCellReformer- (NSDictionary *)reformerDataWithModle:(LMRModel *)model { NSDictionary *resultData = nil; if ([model isKindOfClass:[LMRRecommendBook class]]) { LMRRecommendBook *originModel = (LMRRecommendBook *)model; resultData = @{ kNoCoverCellAuthor:originModel.author, kNoCoverCellTitle: originModel.name, kNoCoverCellCategory:originModel.type, kNoCoverCellCover:originModel.cover, kReformedEntityId:@(originModel.bookId), }; } else if ([model isKindOfClass:[LMRBookInfo class]]) { LMRBookInfo *originModel = (LMRBookInfo *)model; resultData = @{ kNoCoverCellCategory:originModel.bookType, kNoCoverCellTitle:originModel.title, kNoCoverCellAuthor:originModel.author, kNoCoverCellCover:originModel.coverPath, kReformedEntityId:originModel.bookId, }; } else { LMRRankingBook *originModel = (LMRRankingBook *)model; resultData = @{ kNoCoverCellTitle: originModel.name, kNoCoverCellCategory: originModel.type, kNoCoverCellAuthor: originModel.author, kNoCoverCellCover: originModel.cover, kReformedEntityId: @(originModel.bookId), }; } return resultData;}@end
第二种做法就是适配器模式.
用另外一个类专门对不同的Model,进行配.
就像上面的Reformer一样,统一把不同的Model转为dictionary, 然后View装载这个dictionary.
这样通过这个适配器将View和Model直接耦合进行解耦.
缺点: 每个View需要新建一个适配器类. 而且随着不同Model越多来越多. 转换中的if-else也是越来越长.
下面介绍用Protocol来分离View和Model. Protocol很重要的一个作用是分离接口和实现.
比如,我们自定义CustomTableViewCell:
@interface CustomTableViewCell : UITableViewCell@end
@implementation CustomTableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{ if (self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]){ } return self;}@end
然后,为其新建一个CustomTableViewCellProtocol.h. 规定两个接口.
@protocol CustomTableViewCellProtocol <NSObject>- (NSString *)CustomTableViewCellTitle;- (NSString *)CustomTableViewCellDetail;@end
接着,在CustomTableViewCell.h中引入 CustomTableViewCellProtocol.h,并添加装载Model的方法,比如loadModel:,重要声明参数遵守CustomTableViewCellProtocol模式.
#import <UIKit/UIKit.h>#import "CustomTableViewCellProtocol.h"@interface CustomTableViewCell : UITableViewCell- (void)loadModel:(id<CustomTableViewCellProtocol>) model;@end
在CustomTableViewCell.m中实现这个方法:
- (void)loadModel:(id<CustomTableViewCellProtocol>)model{ self.textLabel.text = [model CustomTableViewCellTitle]; self.detailTextLabel.text = [model CustomTableViewCellDetail];}
这样,我们的View已经设置完毕.
对于Model,比如BananaModel.h,声明其遵守CustomTableViewCellProtocol
@interface BananaModel : NSObject<CustomTableViewCellProtocol>@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *introduce;@end
然后,在.m实现这个方法.比如想在 CustomTableViewCell的textLabel显示model的name属性,detailTextLabel显示model的introduce属性.实现如下.
#pragma mark - CustomTableViewProtocol Implement- (NSString *)CustomTableViewCellTitle{ return self.name;}- (NSString *)CustomTableViewCellDetail{ return self.introduce;}
这样Model也设置完毕了.
使用的时候就可以直接装载Model了.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxx" forIndexPath:indexPath]; [cell loadModel:self.dataSource[indexPath.row]]; return cell;}
上面代码中,self.dataSource[indexPath.row]
是BananaModel
.
通过Protocol规定接口,我们成功将View和Model分离了.
任何Model要被装入CustomTableViewCell中,只要遵循CustomTableViewCellProtocol,并实现对应的方法:
- (NSString *)CustomTableViewCellTitle;- (NSString *)CustomTableViewCellDetail;
就可以啦.
- 给CustomTableViewCell规定CustomTableViewCellProtocol接口.然后Model来实现.也就是分离接口和实现的典型.
这样很好地分离了View和Model,又没有恶心的if-else. - 新添加Model的情况下,很多时候我们不需要修改View里面的代码就能适配该Model.
- 不需要添加额外的类,就完成了适配.
- Model不需要继承其他任何类, 由于OC和Swift都是没有多继承特性.
继承是比较宝贵,有些转Model框架需要利用继承例如:JSONModel和Mantle. 滥用继承,可能会导致层次混乱. - 一个Model可以遵循多个Protocol.
这样,我们的Model想被装进不同的View,只要遵循该View的Protocol,并实现对应方法就好了.
文笔拙劣,没看明白请戳demo
对于面向协议开发感兴趣的童鞋可以请戳:swift面向协议开发
不想看英文的童鞋,可以看我简单的翻译,请戳:介绍在Swift2面向协议编程
- 用Protocol来分离的View和Model
- Handlebars 通过JavaScript对view和data的分离来快速构建Web模板
- C 实现HANDLE和VIEW的分离
- qt model和view的table 小记
- Qt 的model和view part2
- Qt 的model和view part1
- backbone model和view
- Qt的Model/View
- Qt 的Model/View
- Qt的model/view
- MVVM模式下,ViewModel和View,Model有什么区别 摘自正美的5群 Model:很简单,就是业务逻辑相关的数据对象,通常从数据库映射而来,我们可以说是与数据库对应的model。
- 用Model来计算cell的高度
- 使用View Model从表现层分离领域模型
- 使用View Model从表现层分离领域模型
- MVC学习1-实现Model—Controller—View分离
- MVC4 Model View Controller分离成独立项目
- 需要解决的问题ionic nvbar 和 view的分离
- 原创:实现view(mxml)和代码分离的好方法
- 软件应届毕业生的2015-7到2016-2的工作总结
- 使用Apache commons-configuration读取配置文件
- cocos2d-x Button的监听使用
- 【NYOJ】[198]数数
- 欢迎使用CSDN-markdown编辑器
- 用Protocol来分离的View和Model
- mongodb 交互式操作和script文件脚本的区别。
- 安卓基础案例 电话+短信
- 【NYOJ】[199]无线网络覆盖
- Android-->Dialog/DialogFragment宽度高度修改/全屏,自定义样式
- composer 详解
- iOS-函数式编程 && 响应式编程概念
- CRT连接Linux
- Codeforces 618C Constellation(简单几何题—叉积)