MVC模式
MVC是Model-View-Controller的縮寫,它將應(yīng)用程序劃分為三個部分:
Model: 模型(用于封裝與應(yīng)用程序的業(yè)務(wù)邏輯相關(guān)的數(shù)據(jù)以及對數(shù)據(jù)的處理方法)
View: 視圖(渲染頁面)
Controller: 控制器(M和V之間的連接器搜囱,用于控制應(yīng)用程序的流程蜀肘,及頁面的業(yè)務(wù)邏輯)
MVC特點(diǎn):
MVC模式的特點(diǎn)在于實現(xiàn)關(guān)注點(diǎn)分離扮宠,即應(yīng)用程序中的數(shù)據(jù)模型與業(yè)務(wù)和展示邏輯解耦狐榔。在客戶端web開發(fā)中,就是將模型(M-數(shù)據(jù)届案、操作數(shù)據(jù))楣颠、視圖(V-顯示數(shù)據(jù)的HTML元素)之間實現(xiàn)代碼分離球碉,松散耦合睁冬,使之成為一個更容易開發(fā)豆拨、維護(hù)和測試的客戶端應(yīng)用程序施禾。
View 傳送指令到 Controller 弥搞;
Controller 完成業(yè)務(wù)邏輯后攀例,要求 Model 改變狀態(tài) 粤铭;
Model 將新的數(shù)據(jù)發(fā)送到 View,用戶得到反饋垛吗。
MVC流程:
MVC流程一共有兩種职烧,在日常開發(fā)中都會使用到蚀之。
一種是通過 View 接受指令捷泞,傳遞給 Controller失受,然后對模型進(jìn)行修改或者查找底層數(shù)據(jù)咏瑟,最后把改動渲染在視圖上码泞。
另一種是通過controller接受指令领铐,傳給Controller:
MVC優(yōu)點(diǎn):
耦合性低绪撵,視圖層和業(yè)務(wù)層分離音诈,這樣就允許更改視圖層代碼而不用重新編譯模型和控制器代碼细溅。
重用性高
生命周期成本低
MVC使開發(fā)和維護(hù)用戶接口的技術(shù)含量降低
可維護(hù)性高谒兄,分離視圖層和業(yè)務(wù)邏輯層也使得WEB應(yīng)用更易于維護(hù)和修改
部署快
MVC缺點(diǎn):
不適合小型,中等規(guī)模的應(yīng)用程序鸥咖,花費(fèi)大量時間將MVC應(yīng)用到規(guī)模并不是很大的應(yīng)用程序通常會得不償失燕鸽。
視圖與控制器間過于緊密連接,視圖與控制器是相互分離啼辣,但卻是聯(lián)系緊密的部件啊研,視圖沒有控制器的存在,其應(yīng)用是很有限的,反之亦然党远,這樣就妨礙了他們的獨(dú)立重用削解。
視圖對模型數(shù)據(jù)的低效率訪問,依據(jù)模型操作接口的不同沟娱,視圖可能需要多次調(diào)用才能獲得足夠的顯示數(shù)據(jù)氛驮。對未變化數(shù)據(jù)的不必要的頻繁訪問矫废,也將損害操作性能律杠。
MVC應(yīng)用:
在web app 流行之初碰声, MVC 就應(yīng)用在了java(struts2)和C#(ASP.NET)服務(wù)端應(yīng)用中瞻颂,后來在客戶端應(yīng)用程序中盖矫,基于MVC模式湃望,AngularJS應(yīng)運(yùn)而生
View
-DemoAView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface DemoAView : UIView
@property(nonatomic ,strong) UILabel *nameLabel;
@end
NS_ASSUME_NONNULL_END
-DemoAView.m
#import "DemoAView.h"
@implementation DemoAView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.nameLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.nameLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.nameLabel];
}
return self;
}
@end
Model
-DemoAModel.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DemoAModel : NSObject
@property (nonatomic ,readonly) NSString *nameString;
@property (nonatomic ,readonly) NSString *ageString;
- (instancetype)initWithNameString:(NSString *)nameString age:(NSString *)ageString;
@end
NS_ASSUME_NONNULL_END
-DemoAModel.m
#import "DemoAModel.h"
@implementation DemoAModel
- (instancetype)initWithNameString:(NSString *)nameString age:(NSString *)ageString{
self = [super init];
if (self) {
_nameString = nameString;
_ageString = ageString;
}
return self;
}
@end
Controller
#import "ViewController.h"
#import "DemoAView.h"
#import "DemoAModel.h"
@interface ViewController ()
@property(nonatomic,strong) DemoAView *demoAView;
@property(nonatomic,strong) DemoAModel *demoAModel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupViews];
self.demoAModel = [[DemoAModel alloc] initWithNameString:@"張三" age:@"20"];
self.demoAView.nameLabel.text = [NSString stringWithFormat:@"姓名:%@----年齡:%@歲",self.demoAModel.nameString,self.demoAModel.ageString];
// Do any additional setup after loading the view.
}
- (void)setupViews{
self.demoAView = [[DemoAView alloc] initWithFrame:CGRectMake(100, 100, self.view.frame.size.width - 200, 100)];
self.demoAView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.demoAView];
}
@end
MVP模式
MVP(Model-View-Presenter)是MVC的改良模式泛源,由IBM的子公司Taligent提出缎玫。和MVC的相同之處在于:Controller/Presenter負(fù)責(zé)業(yè)務(wù)邏輯,Model管理數(shù)據(jù),View負(fù)責(zé)顯示只不過是將 Controller 改名為 Presenter,同時改變了通信方向使碾。
MVP特點(diǎn):
M掐禁、V障本、P之間雙向通信。
View 與 Model 不通信,都通過 Presenter 傳遞。Presenter完全把Model和View進(jìn)行了分離褐捻,主要的程序邏輯在Presenter里實現(xiàn)。
View 非常薄撒璧,不部署任何業(yè)務(wù)邏輯繁调,稱為”被動視圖”(Passive View)萨蚕,即沒有任何主動性岳遥,而 Presenter非常厚,所有邏輯都部署在那里。
Presenter與具體的View是沒有直接關(guān)聯(lián)的昨稼,而是通過定義好的接口進(jìn)行交互牙丽,從而使得在變更View時候可以保持Presenter的不變,這樣就可以重用盖彭。不僅如此纹烹,還可以編寫測試用的View事甜,模擬用戶的各種操作,從而實現(xiàn)對Presenter的測試–從而不需要使用自動化的測試工具滔韵。
與MVC區(qū)別:
在MVP中逻谦,View并不直接使用Model,它們之間的通信是通過Presenter (MVC中的Controller)來進(jìn)行的陪蜻,所有的交互都發(fā)生在Presenter內(nèi)部邦马。
這里寫圖片描述
在MVC中,View會直接從Model中讀取數(shù)據(jù)而不是通過 Controller宴卖。
MVP優(yōu)點(diǎn):
模型與視圖完全分離滋将,我們可以修改視圖而不影響模型;
可以更高效地使用模型症昏,因為所有的交互都發(fā)生在一個地方——Presenter內(nèi)部随闽;
我們可以將一個Presenter用于多個視圖,而不需要改變Presenter的邏輯肝谭。這個特性非常的有用掘宪,因為視圖的變化總是比模型的變化頻繁;
如果我們把邏輯放在Presenter中攘烛,那么我們就可以脫離用戶接口來測試這些邏輯(單元測試)魏滚。
MVP缺點(diǎn):
視圖和Presenter的交互會過于頻繁,使得他們的聯(lián)系過于緊密坟漱。也就是說鼠次,一旦視圖變更了,presenter也要變更芋齿。
MVP應(yīng)用:
可應(yīng)用與Android腥寇、iOS開發(fā)。
MVP的代碼示例部分如下:
- Model
- Model.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface UsrModel : NSObject
@property (nonatomic ,readonly) NSString *nameString;
@property (nonatomic ,readonly) NSString *ageString;
- (instancetype)initWithNameString:(NSString *)nameString age:(NSString *)ageString;
@end
NS_ASSUME_NONNULL_END
- Model.m
#import "UsrModel.h"
@implementation UsrModel
- (instancetype)initWithNameString:(NSString *)nameString age:(NSString *)ageString{
self = [super init];
if (self) {
_nameString = nameString;
_ageString = ageString;
}
return self;
}
@end
- View
- View.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface DemoAView : UIView
@property(nonatomic ,strong) UILabel *nameLabel;
@end
NS_ASSUME_NONNULL_END
- View.m
#import "DemoAView.h"
@implementation DemoAView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.nameLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.nameLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.nameLabel];
}
return self;
}
@end
- Presenter
- Presenter.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol UsrViewProtocol <NSObject>
- (void)setNameString:(NSString *)name;
@end
@interface Presenter : NSObject
- (void)bindView:(id <UsrViewProtocol>)view;
- (void)bindData;
@end
NS_ASSUME_NONNULL_END
- Presenter.m
#import "Presenter.h"
#import "UsrModel.h"
@interface Presenter()
@property (nonatomic, strong) UsrModel *usrModel;
@property (nonatomic, weak) id<UsrViewProtocol> bindView;
@end
@implementation Presenter
- (void)bindView:(id <UsrViewProtocol>)view{
self.bindView = view;
}
- (void)bindData{
self.usrModel = [[UsrModel alloc] initWithNameString:@"李四" age:@"30"];
[self.bindView setNameString:[NSString stringWithFormat:@"姓名:%@----年齡:%@歲",self.usrModel.nameString,self.usrModel.ageString]];
}
@end
Controller
#import "ViewController.h"
#import "DemoAView.h"
#import "Presenter.h"
@interface ViewController () <UsrViewProtocol>
@property(nonatomic,strong) DemoAView *demoAView;
@property(nonatomic,strong) Presenter *presenter;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupViews];
self.presenter = [Presenter new];
[self.presenter bindView:self];
[self.presenter bindData];
}
- (void)setupViews{
self.demoAView = [[DemoAView alloc] initWithFrame:CGRectMake(100, 100, self.view.frame.size.width - 200, 100)];
self.demoAView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.demoAView];
}
#pragma -mark ViewProtocol
- (void)setNameString:(NSString *)name{
self.demoAView.nameLabel.text = name;
}
@end
MVVM模式
MVVM是Model-View-ViewModel的簡寫觅捆。微軟的WPF(Windows Presentation Foundation–微軟推出的基于Windows 的用戶界面框架)帶來了新的技術(shù)體驗, 使得軟件UI層更加細(xì)節(jié)化赦役、可定制化。與此同時惠拭,在技術(shù)層面扩劝,WPF也帶來了 諸如Binding(綁定)、Dependency Property(依賴屬性)职辅、Routed Events(路由事件)棒呛、Command(命令)、DataTemplate(數(shù)據(jù)模板)域携、ControlTemplate(控制模板)等新特性簇秒。MVVM模式其實是MV模式與WPF結(jié)合的應(yīng)用方式時發(fā)展演變過來的一種新型架構(gòu)模式。它立足于原有MVP框架并且把WPF的新特性糅合進(jìn)去秀鞭,以應(yīng)對客戶日益復(fù)雜的需求變化趋观。
MVVM優(yōu)點(diǎn):
MVVM模式和MVC模式類似扛禽,主要目的是分離視圖(View)和模型(Model),有幾大優(yōu)點(diǎn):
1.低耦合皱坛,視圖(View)可以獨(dú)立于Model變化和修改编曼,一個ViewModel可以綁定到不同的”View”上,當(dāng)View變化的時候Model可以不變剩辟,當(dāng)Model變化的時候View也可以不變掐场。
2.可重用性,可以把一些視圖邏輯放在一個ViewModel里面贩猎,讓很多view重用這段視圖邏輯熊户。
3.獨(dú)立開發(fā),開發(fā)人員可以專注于業(yè)務(wù)邏輯和數(shù)據(jù)的開發(fā)(ViewModel)吭服,設(shè)計人員可以專注于頁面設(shè)計嚷堡,使用Expression Blend可以很容易設(shè)計界面并生成xml代碼。
4.可測試艇棕,界面向來是比較難于測試的蝌戒,而現(xiàn)在測試可以針對ViewModel來寫。
- Model
- DemoAView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface DemoAView : UIView
@property(nonatomic ,strong) UILabel *nameLabel;
@end
NS_ASSUME_NONNULL_END
- DemoAView.m
#import "DemoAView.h"
@implementation DemoAView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.nameLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.nameLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.nameLabel];
}
return self;
}
@end
- Model
- UsrModel.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface UsrModel : NSObject
@property (nonatomic ,readonly) NSString *nameString;
@property (nonatomic ,readonly) NSString *ageString;
- (instancetype)initWithNameString:(NSString *)nameString age:(NSString *)ageString;
@end
NS_ASSUME_NONNULL_END
- UsrModel.m
#import "UsrModel.h"
@implementation UsrModel
- (instancetype)initWithNameString:(NSString *)nameString age:(NSString *)ageString{
self = [super init];
if (self) {
_nameString = nameString;
_ageString = ageString;
}
return self;
}
@end
- ViewModel
- ViewModel.h
#import <Foundation/Foundation.h>
#import "UsrModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface UsrViewModel : NSObject
@property (nonatomic, readonly) UsrModel *usr;
@property (nonatomic, readonly) NSString *name;
- (instancetype)initWithModel:(UsrModel *)usr;
@end
NS_ASSUME_NONNULL_END
- ViewModel.m
#import "UsrViewModel.h"
@implementation UsrViewModel
- (instancetype)initWithModel:(UsrModel *)usr{
self = [super init];
if (self) {
_usr = usr;
_name = [NSString stringWithFormat:@"姓名:%@----年齡:%@",_usr.nameString,_usr.ageString];
}
return self;
}
@end
Controller
#import "ViewController.h"
#import "DemoAView.h"
#import "UsrViewModel.h"
#import "UsrModel.h"
@interface ViewController ()
@property(nonatomic,strong) DemoAView *demoAView;
@property (nonatomic, strong) UsrViewModel *viewModel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupViews];
UsrModel *usr = [[UsrModel alloc] initWithNameString:@"王五" age:@"35"];
self.viewModel = [[UsrViewModel alloc] initWithModel:usr];
self.demoAView.nameLabel.text = self.viewModel.name;
}
- (void)setupViews{
self.demoAView = [[DemoAView alloc] initWithFrame:CGRectMake(100, 100, self.view.frame.size.width - 200, 100)];
self.demoAView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.demoAView];
}
@end
最終效果如下圖