废话不多说,直接上代码
我们的model:User,可以看出来和MVC里没有区别
#import <Foundation/Foundation.h>
@interface User : NSObject
+ (instancetype)userWithFirstName:(NSString *)firstName lastName:(NSString *)lastName email:(NSString *)email age:(NSInteger )age;
@property (copy, nonatomic) NSString *firstName;
@property (copy, nonatomic) NSString *lastName;
@property (copy, nonatomic) NSString *email;
@property (nonatomic) NSInteger age;
@end
#import "User.h"
@implementation User
+ (instancetype)userWithFirstName:(NSString *)firstName lastName:(NSString *)lastName email:(NSString *)email age:(NSInteger )age {
return [[self alloc] initWithFirstName:firstName lastName:lastName email:email age:age];
}
- (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName email:(NSString *)email age:(NSInteger )age
{
self = [super init];
if (self) {
_firstName = firstName;
_lastName = lastName;
_email = email;
_age = age;
}
return self;
}
@end
这里我们增加了一个服务器层,用于取网络数据模拟UserService
,看到头文件我突然想起了之前在一家公司写代码,就是这样的效果,你完全不知道是请求的网络还是本地数据,你也完全不用创建User模型 ,你需要做的只是调用一个类的一个方法,你就能得到一个对象,或者一堆对象。这就是站在UI层写代码的便捷之处。
#import <Foundation/Foundation.h>
@class User;
@interface UserService : NSObject
- (void)getUsers:(void (^)(NSArray<User *> *users))usersBlock;
@end
#import "UserService.h"
#import "User.h"
@implementation UserService
- (void)getUsers:(void (^)(NSArray<User *> *users))usersBlock {
NSInteger userCount = 3;
NSMutableArray<User *> *users = [NSMutableArray arrayWithCapacity:userCount];
for (NSInteger i = 0; i < userCount; ++i) {
NSString *idxStr = [NSString stringWithFormat:@"%zd", i];
User *user =
[User userWithFirstName:idxStr lastName:idxStr email:idxStr age:i];
[users addObject:user];
}
double delay = 2;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (usersBlock) {
usersBlock(users);
}
});
}
@end
然后我们会有一个展示层UserPresenter
我们定义了一个类UserViewData
,类似MVVM里经过最原始的model处理过后得到的viewModel一样,当然MVVM里好像是把网络层放到了viewModel里,这里MVP有个专门的服务类来做这类事情 。这个UserViewData
是userView
能直接用的模型
我们还定义了一个协议UserViewDelegate
,用于处理,加载数据和完成加载数据和设置数据源和为空时的处理。
然后我们的展示层UserPresenter
,要展示当然是要有数据,所以 UserPresenter
的初始化方法依赖于UserService
,当然有了数据之后,可以展示与不展示,这里有个userViewDelegate
属性。还有个getUsers
方法,在调用这个方法里就会调用
userViewDelegate
里的加载完成加载设置数据和空数据之类的操作。相对于MVC来说的确代码量上去了。项目小可能感受不到好处,项目大了,业务复杂了可能就有体会了,当你看到别人写2500行的控制器时可能就会思考其他模式来进行优化了,其实模式是可以共存在一个项目里,针对不同的模块选用不同的模式。
简单的MVC,复杂的后期需要扩展的可用MVP、MVVM等。之前好像听一个朋友说VIP
模式,真是吊啊,什么模式都有,只怪我们太年少,不努力学习。
import <Foundation/Foundation.h>
@class UserService;
@interface UserViewData: NSObject
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *age;
+ (instancetype)userViewDataWithName:(NSString *)name age:(NSString *)age;
@end
@protocol UserViewDelegate
- (void)startLoading;
- (void)finishLoading;
- (void)setUsers:(NSArray<UserViewData *> *)userViewDatas;
- (void)setEmptyUsers;
@end
@interface UserPresenter : NSObject
+ (instancetype)userPresenterWithUserService:(UserService *)userService;
@property(nonatomic, weak) id<UserViewDelegate> userViewDelegate;
- (void)getUsers;
@end
#import "UserPresenter.h"
#import "UserService.h"
#import "User.h"
@implementation UserViewData
+ (instancetype)userViewDataWithName:(NSString *)name age:(NSString *)age {
return [[self alloc] initWithName:name age:age];
}
- (instancetype)initWithName:(NSString *)name age:(NSString *)age {
self = [super init];
if (self) {
_name = name;
_age = age;
}
return self;
}
@end
@interface UserPresenter()
@property(nonatomic, strong) UserService *userService;
@end
@implementation UserPresenter
+ (instancetype)userPresenterWithUserService:(UserService *)userService
{
return [[self alloc] initWithUserService:userService];
}
- (instancetype)initWithUserService:(UserService *)userService
{
self = [super init];
if (self) {
_userService = userService;
}
return self;
}
-(void)attachView:(id<UserViewDelegate>)userView {
_userViewDelegate = userView;
}
-(void)detachView {
_userViewDelegate = nil;
}
- (void)getUsers {
[self.userViewDelegate startLoading];
__weak __typeof(self)weakSelf = self;
[self.userService getUsers:^(NSArray *users) {
[weakSelf.userViewDelegate finishLoading];
if (users.count == 0) {
[self.userViewDelegate setEmptyUsers];
} else {
NSMutableArray<UserViewData *> *userViewDatas = [NSMutableArray arrayWithCapacity:3];
for (User *user in users) {
NSString *ageStr = [NSString stringWithFormat:@"%zd", user.age];
UserViewData *userViewData = [UserViewData userViewDataWithName:user.firstName age:ageStr];
[userViewDatas addObject:userViewData];
}
[self.userViewDelegate setUsers:userViewDatas];
}
}];
}
@end
最后控制器里就是一些view的处理及代理实现,拼装一些类。
image.png
#import "ViewController.h"
#import "UserPresenter.h"
#import "UserService.h"
@interface ViewController ()<UITableViewDataSource, UserViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *emptyView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator;
@property(nonatomic, strong) UserPresenter *userPresenter;
@property(nonatomic, strong) NSArray<UserViewData *> *userViewDatas;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.indicator.hidesWhenStopped = YES;
self.userPresenter = [UserPresenter userPresenterWithUserService:[UserService new]];
self.userPresenter.userViewDelegate = self;
[self.userPresenter getUsers];
}
#pragma mark - UserViewDelegate
-(void)startLoading {
[self.indicator startAnimating];
}
-(void)finishLoading {
[self.indicator stopAnimating];
}
-(void)setUsers:(NSArray<UserViewData *> *)userViewDatas {
self.userViewDatas = userViewDatas;
self.tableView.hidden = NO;
self.emptyView.hidden = YES;
[self.tableView reloadData];
}
-(void)setEmptyUsers {
self.tableView.hidden = YES;
self.emptyView.hidden = NO;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.userViewDatas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIDStr = @"user";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIDStr];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIDStr];
}
UserViewData *userViewData = self.userViewDatas[indexPath.row];
cell.textLabel.text = userViewData.name;
cell.detailTextLabel.text = userViewData.age;
return cell;
}
@end
网友评论