1、MVC
Apple官方最標(biāo)準(zhǔn)的是UITableViewController
// VC里面對(duì)view進(jìn)行賦值。
cell.textLabel.text = model.title;
delegate
2、MVC變種
view.model = model;
view.delegate = self;
3、MVP
VC任命一個(gè)presenter幫他處理相關(guān)業(yè)務(wù),實(shí)現(xiàn)代碼拆分。相當(dāng)于manager
// VC中
self.presenter = [[MJAppPresenter alloc] initWithController:self];
// presenter里面處理view、model、delegate
- (instancetype)initWithController:(UIViewController *)controller
{
if (self = [super init]) {
self.controller = controller;
// 創(chuàng)建View
MJAppView *appView = [[MJAppView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)];
appView.delegate = self;
[controller.view addSubview:appView];
// 加載模型數(shù)據(jù)
MJApp *app = [[MJApp alloc] init];
app.name = @"QQ";
app.image = @"QQ";
// 賦值數(shù)據(jù)
[appView setName:app.name andImage:app.image];
// appView.iconView.image = [UIImage imageNamed:app.image];
// appView.nameLabel.text = app.name;
}
return self;
}
#pragma mark - MJAppViewDelegate
- (void)appViewDidClick:(MJAppView *)appView
{
NSLog(@"presenter 監(jiān)聽(tīng)了 appView 的點(diǎn)擊");
}
3、MVVM
相當(dāng)于MVP中的P換成了VM,然后加了監(jiān)聽(tīng)機(jī)制(RAC或KVO)
// ViewModel
- (instancetype)initWithController:(UIViewController *)controller
{
if (self = [super init]) {
self.controller = controller;
// 創(chuàng)建View
MJAppView *appView = [[MJAppView alloc] init];
appView.frame = CGRectMake(100, 100, 100, 150);
appView.delegate = self;
appView.viewModel = self;
[controller.view addSubview:appView];
// 加載模型數(shù)據(jù)
MJApp *app = [[MJApp alloc] init];
app.name = @"QQ";
app.image = @"QQ";
// 設(shè)置數(shù)據(jù)
self.name = app.name;
self.image = app.image;
}
return self;
}
// View
- (void)setViewModel:(MJAppViewModel *)viewModel
{
_viewModel = viewModel;
__weak typeof(self) waekSelf = self;
[self.KVOController observe:viewModel keyPath:@"name" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
waekSelf.nameLabel.text = change[NSKeyValueChangeNewKey];
}];
[self.KVOController observe:viewModel keyPath:@"image" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
waekSelf.iconView.image = [UIImage imageNamed:change[NSKeyValueChangeNewKey]];
}];
}
4、分層
界面層- 新聞列表,tableview
業(yè)務(wù)層- 加載展示數(shù)據(jù),一些業(yè)務(wù)邏輯
數(shù)據(jù)層- 網(wǎng)絡(luò)或本地
5、設(shè)計(jì)模式
比架構(gòu)小,關(guān)注類(lèi)與類(lèi)之間的關(guān)系,主要有六大原則:依賴(lài)倒置、單一職責(zé)、最小知道、開(kāi)閉、里氏替換、迪米特(鄰近)