设计模式

用Protocol来分离的View和Model

2017-08-08  本文已影响30人  iDeveloper

在项目中,不同的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;

就可以啦.

文笔拙劣,没看明白请戳demo

上一篇 下一篇

猜你喜欢

热点阅读