MVC的傳統解釋
Model-View-ViewModel 的架構替換 Model-View-Controller
我們看到的是一個典型的 MVC 設置。Model 呈現數據,View 呈現用戶界面,而 View Controller 調節它兩者之間的交互。Cool
MVC的進一步解釋
稍微考慮一下,雖然 View 和 View Controller 是技術上不同的組件,但它們幾乎總是手牽手在一起,成對的。你什么時候看到一個 View 能夠與不同 View Controller 配對?或者反過來?所以,為什么不正規化它們的連接呢?
MVVM的形成
這更準確地描述了你可能已經編寫的 MVC 代碼。但它并沒有做太多事情來解決 iOS 應用中日益增長的重量級視圖控制器的問題。在典型的 MVC 應用里,許多邏輯被放在 View Controller 里。它們中的一些確實屬于 View Controller,但更多的是所謂的“表示邏輯(presentation logic)”,以 MVVM 屬術語來說,就是那些將 Model 數據轉換為 View 可以呈現的東西的事情,例如將一個 NSDate 轉換為一個格式化過的 NSString。
我們的圖解里缺少某些東西,那些使我們可以把所有表示邏輯放進去的東西。我們打算將其稱為 “View Model” —— 它位于 View/Controller 與 Model 之間:
看起好多了!這個圖解準確地描述了什么是 MVVM:一個 MVC 的增強版,我們正式連接了視圖和控制器,并將表示邏輯從 Controller 移出放到一個新的對象里,即 View Model。MVVM 聽起來很復雜,但它本質上就是一個精心優化的 MVC 架構,而 MVC 你早已熟悉。
現在我們知道了什么是 MVVM,但為什么我們會想要去使用它呢?在 iOS 上使用 MVVM 的動機,對我來說,無論如何,就是它能減少 View Controller 的復雜性并使得表示邏輯更易于測試。通過一些例子,我們將看到它如何達到這些目標。
此處有三個重點是我希望你看完本文能帶走的:
1 . MVVM 可以兼容你當下使用的 MVC 架構。
2 . MVVM 增加你的應用的可測試性。
3 . MVVM 配合一個綁定機制效果最好。
如我們之前所見,MVVM 基本上就是 MVC 的改進版,所以很容易就能看到它如何被整合到現有使用典型 MVC 架構的應用中。讓我們看一個簡單的 Person Model 以及相應的 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!現在我們假設我們有一個 PersonViewController ,在 viewDidLoad 里,只需要基于它的 model 屬性設置一些 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];
}
這全都直截了當,標準的 MVC。
現在來看看我們如何用一個 View Model 來增強它。
@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 的實現大概如下:
@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!我們已經將 viewDidLoad 中的表示邏輯放入我們的 View Model 里了。此時,我們新的 viewDidLoad 就會非常輕量:
- (void)viewDidLoad {
[super viewDidLoad];
self.nameLabel.text = self.viewModel.nameText;
self.birthdateLabel.text = self.viewModel.birthdateText;
}
所以,如你所見,并沒有對我們的 MVC 架構做太多改變。還是同樣的代碼,只不過移動了位置。它與 MVC 兼容,帶來更輕量的 View Controllers。
可測試,嗯?是怎樣?好吧,View Controller 是出了名的難以測試,因為它們做了太多事情。在 MVVM 里,我們試著盡可能多的將代碼移入 View Model 里。測試 View Controller 就變得容易多了,因為它們不再做一大堆事情,并且 View Model 也非常易于測試。