iOS MVC和MVP详解
2020-05-28 本文已影响0人
ios小蜗牛
常规MVC
优点:
1.Model可复用
2.View可复用
3.耦合性低
缺点:
1.控制器臃肿
2.View控件暴露在外,不安全,封装性差
Model的实现
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MVCModel : NSObject
/** url */
@property (nonatomic, copy) NSString *photoUrl;
/** title */
@property (nonatomic, copy) NSString *titleStr;
@end
NS_ASSUME_NONNULL_END
View的实现
// 头文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MVCView : UIView
/** 图片 */
@property (nonatomic, strong) UIImageView *imageView;
/** 文本 */
@property (nonatomic, strong) UILabel *titleLbl;
@end
NS_ASSUME_NONNULL_END
// 实现文件
#import "MVCView.h"
@implementation MVCView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self createUI];
}
return self;
}
/**
创建控件
*/
- (void)createUI{
UIImageView *imageView = [[UIImageView alloc] init];
[self addSubview:imageView];
self.imageView = imageView;
UILabel *titleLbl = [[UILabel alloc] init];
titleLbl.textColor = [UIColor blackColor];
titleLbl.font = [UIFont systemFontOfSize:16];
titleLbl.textAlignment = NSTextAlignmentCenter;
[self addSubview:titleLbl];
self.titleLbl = titleLbl;
}
- (void)layoutSubviews{
[super layoutSubviews];
self.imageView.frame = CGRectMake(0, 0, 100, 100);
self.titleLbl.frame = CGRectMake(0, 100, 100, 50);
}
@end
Controller的实现
#import "MVCViewController.h"
#import "MVCView.h"
#import "MVCModel.h"
@interface MVCViewController ()
@end
@implementation MVCViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
}
#pragma mark - 搭建界面
- (void)createUI{
// 创建视图
MVCView *view = [[MVCView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)];
[self.view addSubview:view];
// 创建model
MVCModel *model = [[MVCModel alloc] init];
model.photoUrl = @"classStudent";
model.titleStr = @"早自习";
// 赋值
view.imageView.image = [UIImage imageNamed:model.photoUrl];
view.titleLbl.text = model.titleStr;
}
@end
MVC变种
优点:
1.减少控制器的代码量
2.View的封装性强,外界无法看到内部控件实现
缺点:
1.耦合性变高
2.model,view无法单独复用
Model的实现
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface VariantModel : NSObject
/** url */
@property (nonatomic, copy) NSString *photoUrl;
/** title */
@property (nonatomic, copy) NSString *titleStr;
@end
NS_ASSUME_NONNULL_END
View的实现
// 头文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class VariantModel;
@interface VariantView : UIView
/** model */
@property (nonatomic, strong) VariantModel *model;
@end
NS_ASSUME_NONNULL_END
// 实现文件
#import "VariantView.h"
#import "VariantModel.h"
@interface VariantView ()
/** 图片 */
@property (nonatomic, strong) UIImageView *imageView;
/** 文本 */
@property (nonatomic, strong) UILabel *titleLbl;
@end
@implementation VariantView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self createUI];
}
return self;
}
/**
创建控件
*/
- (void)createUI{
UIImageView *imageView = [[UIImageView alloc] init];
[self addSubview:imageView];
self.imageView = imageView;
UILabel *titleLbl = [[UILabel alloc] init];
titleLbl.textColor = [UIColor blackColor];
titleLbl.font = [UIFont systemFontOfSize:16];
titleLbl.textAlignment = NSTextAlignmentCenter;
[self addSubview:titleLbl];
self.titleLbl = titleLbl;
}
/**
设置frame
*/
- (void)layoutSubviews{
[super layoutSubviews];
self.imageView.frame = CGRectMake(0, 0, 100, 100);
self.titleLbl.frame = CGRectMake(0, 100, 100, 50);
}
/**
赋值
*/
- (void)setModel:(VariantModel *)model{
_model = model;
self.imageView.image = [UIImage imageNamed:model.photoUrl];
self.titleLbl.text = model.titleStr;
}
Controller的实现
#import "VariantViewController.h"
#import "VariantModel.h"
#import "VariantView.h"
@interface VariantViewController ()
@end
@implementation VariantViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
}
#pragma mark - 搭建界面
- (void)createUI{
// 创建视图
VariantView *view = [[VariantView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)];
[self.view addSubview:view];
// 创建model
VariantModel *model = [[VariantModel alloc] init];
model.photoUrl = @"classStudent";
model.titleStr = @"中自习";
// 赋值
view.model = model;
}
@end
MVP
优点:
1.Model可复用
2.View可复用
3.耦合性低
4.减少控制器的代码量
缺点:
1.总体代码量增大
Model的实现
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MVPModel : NSObject
/** url */
@property (nonatomic, copy) NSString *photoUrl;
/** title */
@property (nonatomic, copy) NSString *titleStr;
@end
NS_ASSUME_NONNULL_END
View的实现
// 头文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MVPView : UIView
/** 图片 */
@property (nonatomic, strong) UIImageView *imageView;
/** 文本 */
@property (nonatomic, strong) UILabel *titleLbl;
@end
NS_ASSUME_NONNULL_END
// 实现文件
#import "MVPView.h"
@implementation MVPView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self createUI];
}
return self;
}
/**
创建控件
*/
- (void)createUI{
UIImageView *imageView = [[UIImageView alloc] init];
[self addSubview:imageView];
self.imageView = imageView;
UILabel *titleLbl = [[UILabel alloc] init];
titleLbl.textColor = [UIColor blackColor];
titleLbl.font = [UIFont systemFontOfSize:16];
titleLbl.textAlignment = NSTextAlignmentCenter;
[self addSubview:titleLbl];
self.titleLbl = titleLbl;
}
- (void)layoutSubviews{
[super layoutSubviews];
self.imageView.frame = CGRectMake(0, 0, 100, 100);
self.titleLbl.frame = CGRectMake(0, 100, 100, 50);
}
@end
Controller的实现
#import "MVPViewController.h"
#import "MVPPresenter.h"
@interface MVPViewController ()
@property (strong, nonatomic) MVPPresenter *presenter;
@end
@implementation MVPViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
}
#pragma mark - 搭建界面
- (void)createUI{
self.presenter = [[MVPPresenter alloc] initWithViewController:self];
}
@end
Presenter的实现
// 头文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MVPPresenter : NSObject
/**
启动方法
*/
- (instancetype)initWithViewController:(UIViewController *)controller;
@end
NS_ASSUME_NONNULL_END
// 实现文件
#import "MVPPresenter.h"
#import "MVPView.h"
#import "MVPModel.h"
@interface MVPPresenter ()
/** viewController */
@property (nonatomic, weak) UIViewController *controller;
@end
@implementation MVPPresenter
- (instancetype)initWithViewController:(UIViewController *)controller{
if (self = [super init]) {
self.controller = controller;
[self createUI];
}
return self;
}
/**
创建控件
*/
- (void)createUI{
// 创建视图
MVPView *view = [[MVPView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)];
[self.controller.view addSubview:view];
// 创建model
MVPModel *model = [[MVPModel alloc] init];
model.photoUrl = @"classStudent";
model.titleStr = @"晚自习";
// 赋值
view.imageView.image = [UIImage imageNamed:model.photoUrl];
view.titleLbl.text = model.titleStr;
}
@end