MVC的傳統(tǒng)解釋
Model-View-ViewModel 的架構(gòu)替換 Model-View-Controller
我們看到的是一個典型的 MVC 設(shè)置。Model 呈現(xiàn)數(shù)據(jù)谆趾,View 呈現(xiàn)用戶界面躁愿,而 View Controller 調(diào)節(jié)它兩者之間的交互。Cool
MVC的進(jìn)一步解釋
稍微考慮一下沪蓬,雖然 View 和 View Controller 是技術(shù)上不同的組件彤钟,但它們幾乎總是手牽手在一起,成對的怜跑。你什么時候看到一個 View 能夠與不同 View Controller 配對样勃?或者反過來?所以性芬,為什么不正規(guī)化它們的連接呢峡眶?
MVVM的形成
這更準(zhǔn)確地描述了你可能已經(jīng)編寫的 MVC 代碼。但它并沒有做太多事情來解決 iOS 應(yīng)用中日益增長的重量級視圖控制器的問題植锉。在典型的 MVC 應(yīng)用里辫樱,許多邏輯被放在 View Controller 里。它們中的一些確實屬于 View Controller俊庇,但更多的是所謂的“表示邏輯(presentation logic)”狮暑,以 MVVM 屬術(shù)語來說鸡挠,就是那些將 Model 數(shù)據(jù)轉(zhuǎn)換為 View 可以呈現(xiàn)的東西的事情,例如將一個 NSDate 轉(zhuǎn)換為一個格式化過的 NSString搬男。
我們的圖解里缺少某些東西拣展,那些使我們可以把所有表示邏輯放進(jìn)去的東西。我們打算將其稱為 “View Model” —— 它位于 View/Controller 與 Model 之間:
看起好多了缔逛!這個圖解準(zhǔn)確地描述了什么是 MVVM:一個 MVC 的增強(qiáng)版备埃,我們正式連接了視圖和控制器,并將表示邏輯從 Controller 移出放到一個新的對象里褐奴,即 View Model按脚。MVVM 聽起來很復(fù)雜,但它本質(zhì)上就是一個精心優(yōu)化的 MVC 架構(gòu)敦冬,而 MVC 你早已熟悉辅搬。
現(xiàn)在我們知道了什么是 MVVM,但為什么我們會想要去使用它呢脖旱?在 iOS 上使用 MVVM 的動機(jī)堪遂,對我來說,無論如何夯缺,就是它能減少 View Controller 的復(fù)雜性并使得表示邏輯更易于測試蚤氏。通過一些例子,我們將看到它如何達(dá)到這些目標(biāo)踊兜。
此處有三個重點是我希望你看完本文能帶走的:
1 . MVVM 可以兼容你當(dāng)下使用的 MVC 架構(gòu)竿滨。
2 . MVVM 增加你的應(yīng)用的可測試性。
3 . MVVM 配合一個綁定機(jī)制效果最好捏境。
如我們之前所見于游,MVVM 基本上就是 MVC 的改進(jìn)版,所以很容易就能看到它如何被整合到現(xiàn)有使用典型 MVC 架構(gòu)的應(yīng)用中垫言。讓我們看一個簡單的 Person Model 以及相應(yīng)的 View Controller:
@interface Person : NSObject
- (instancetype)initwithSalutation:(NSString *)salutation firstName:(NSString *)firstName lastName:(NSString *)lastName birthdate:(NSDate *)birthdate;
@property (nonatomic, readonly) NSString *salutation;
@property (nonatomic, readonly) NSString *firstName;
@property (nonatomic, readonly) NSString *lastName;
@property (nonatomic, readonly) NSDate *birthdate;
@end
Cool贰剥!現(xiàn)在我們假設(shè)我們有一個 PersonViewController ,在 viewDidLoad 里筷频,只需要基于它的 model 屬性設(shè)置一些 Label 即可蚌成。
- (void)viewDidLoad {
[super viewDidLoad];
if (self.model.salutation.length > 0) {
self.nameLabel.text = [NSString stringWithFormat:@"%@ %@ %@", self.model.salutation, self.model.firstName, self.model.lastName];
} else {
self.nameLabel.text = [NSString stringWithFormat:@"%@ %@", self.model.firstName, self.model.lastName];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE MMMM d, yyyy"];
self.birthdateLabel.text = [dateFormatter stringFromDate:model.birthdate];
}
這全都直截了當(dāng),標(biāo)準(zhǔn)的 MVC凛捏。
現(xiàn)在來看看我們?nèi)绾斡靡粋€ View Model 來增強(qiáng)它担忧。
@interface PersonViewModel : NSObject
- (instancetype)initWithPerson:(Person *)person;
@property (nonatomic, readonly) Person *person;
@property (nonatomic, readonly) NSString *nameText;
@property (nonatomic, readonly) NSString *birthdateText;
@end
我們的 View Model 的實現(xiàn)大概如下:
@implementation PersonViewModel
- (instancetype)initWithPerson:(Person *)person {
self = [super init];
if (!self) return nil;
_person = person;
if (person.salutation.length > 0) {
_nameText = [NSString stringWithFormat:@"%@ %@ %@", self.person.salutation, self.person.firstName, self.person.lastName];
} else {
_nameText = [NSString stringWithFormat:@"%@ %@", self.person.firstName, self.person.lastName];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE MMMM d, yyyy"];
_birthdateText = [dateFormatter stringFromDate:person.birthdate];
return self;
}
@end
Cool!我們已經(jīng)將 viewDidLoad 中的表示邏輯放入我們的 View Model 里了坯癣。此時瓶盛,我們新的 viewDidLoad 就會非常輕量:
- (void)viewDidLoad {
[super viewDidLoad];
self.nameLabel.text = self.viewModel.nameText;
self.birthdateLabel.text = self.viewModel.birthdateText;
}
所以,如你所見,并沒有對我們的 MVC 架構(gòu)做太多改變惩猫。還是同樣的代碼芝硬,只不過移動了位置。它與 MVC 兼容轧房,帶來更輕量的 View Controllers拌阴。
可測試,嗯锯厢?是怎樣皮官?好吧,View Controller 是出了名的難以測試实辑,因為它們做了太多事情。在 MVVM 里藻丢,我們試著盡可能多的將代碼移入 View Model 里剪撬。測試 View Controller 就變得容易多了,因為它們不再做一大堆事情悠反,并且 View Model 也非常易于測試残黑。