IOS Controller瘦身二:面向协议编程MVP模式,对M和V进行解耦

释放双眼,带上耳机,听听看~!

前言:

续上一篇 IOS Controller瘦身一:剥离UITableView,封装dataSource和deleagte。写如何封装UITableView的数据源和代理源以达到对C瘦身以及解耦目的后,又一篇对C瘦身以及解耦的文章,大家可整合两篇进行使用,基本可以使C变得好看很多,不显得辣么臃肿。

MVP设计模式:

1、数据请求放于M里面

2、新增一个modelPresent类,用于处理逻辑,并且编写一个协议,让C进行遵守此协议,以达到C和present交互的效果

3、好处是C里面没了数据请求,只要遵守协议就能拿到数据,并且V和M之间进行了完全的解耦

代码展示:

Model.h文件

#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface UserModel : NSObject
@property (nonatomic, assign)NSInteger age;
@property (nonatomic, copy)NSString *name;
@property (nonatomic, copy)NSString *email;
- (instancetype)initWithAge:(NSInteger)age name:(NSString *)name email:(NSString *)email;
///模拟获取数据
+ (void)getUser:(void(^)(NSArray <UserModel *> *users))usersBlock;
@end
NS_ASSUME_NONNULL_END

Model.m文件

#import "UserModel.h"
@implementation UserModel
- (instancetype)initWithAge:(NSInteger)age name:(NSString *)name email:(NSString *)email
{
self = [super init];
if (self) {
_age = age;
_name = name;
_email = email;
}
return self;
}
+ (void)getUser:(void(^)(NSArray <UserModel *> *users))usersBlock
{
//11行
NSInteger count = 10;
NSMutableArray<UserModel *> *users = [NSMutableArray arrayWithCapacity:count];
for (int i = 0 ; i < count; i++) {
NSString *indexStr = [NSString stringWithFormat:@"%ld",(long)i];
UserModel *user = [[UserModel alloc]initWithAge:i name:indexStr email:indexStr];
[users addObject:user];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (usersBlock) {
usersBlock(users);
}
});
}
@end

modelPresent.h文件

#import <Foundation/Foundation.h>
#import "UserModel.h"
NS_ASSUME_NONNULL_BEGIN
///建议协议另建一个protocol文件方便管理
@protocol UserViewDelegate <NSObject>
@optional
- (void)startLoading;
- (void)finishLoading;
///数据为空时
- (void)setEmptyUsers;
@required
- (void)setUsers:(NSArray<UserModel *> *)users;
@end
@interface UserModelPresent : NSObject
@property (nonatomic, weak)id <UserViewDelegate> delegate;
///逻辑启动入口
- (void)getUsers;
@end
NS_ASSUME_NONNULL_END

modelPresent.m文件

#import "UserModelPresent.h"
@implementation UserModelPresent
- (void)getUsers
{
__weak typeof(self) weakSelf = self;
[UserModel getUser:^(NSArray<UserModel *> * _Nonnull users) {
[weakSelf.delegate finishLoading];
BOOL isUsers = users && users.count;
if (!isUsers) {
[weakSelf.delegate setEmptyUsers];
}else{
if ([weakSelf.delegate respondsToSelector:@selector(setUser:)]) {
[weakSelf.delegate setUsers:users];
}
}
}];
}
@end

controller调用

#import "ViewController.h"
#import "UserModelPresent.h"
@interface ViewController ()<UserViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#pragma mark - <UserViewDelegate>
- (void)startLoading
{
NSLog(@"开始操作");
}
- (void)finishLoading
{
NSLog(@"结束操作");
}
- (void)setEmptyUsers
{
NSLog(@"数据为空");
}
- (void)setUsers:(NSArray<UserModel *> *)users
{
NSLog(@"接收数据");
}
@end

欢迎各位看官提出不足之处以及更好的解决方案

人已赞赏
iOS文章

IOS Controller瘦身一:剥离UITableView,封装dataSource和deleagte。

2021-1-14 20:46:22

iOS文章

iOS时间与日期处理

2021-1-14 22:01:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索