iOS-架构设计

1、MVC

Apple官方最标准的是UITableViewController

// VC里面对view进行赋值。
cell.textLabel.text = model.title;
delegate

2、MVC变种

view.model = model;
view.delegate = self;

3、MVP

VC任命一个presenter帮他处理相关业务,实现代码拆分。相当于manager

// VC中
self.presenter = [[MJAppPresenter alloc] initWithController:self];
// presenter里面处理view、model、delegate
- (instancetype)initWithController:(UIViewController *)controller
{
    if (self = [super init]) {
        self.controller = controller;
        
        // 创建View
        MJAppView *appView = [[MJAppView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)];
        appView.delegate = self;
        [controller.view addSubview:appView];
        
        // 加载模型数据
        MJApp *app = [[MJApp alloc] init];
        app.name = @"QQ";
        app.image = @"QQ";
        
        // 赋值数据
        [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 监听了 appView 的点击");
}

3、MVVM

相当于MVP中的P换成了VM,然后加了监听机制(RAC或KVO)

// ViewModel
- (instancetype)initWithController:(UIViewController *)controller
{
    if (self = [super init]) {
        self.controller = controller;
        
        // 创建View
        MJAppView *appView = [[MJAppView alloc] init];
        appView.frame = CGRectMake(100, 100, 100, 150);
        appView.delegate = self;
        appView.viewModel = self;
        [controller.view addSubview:appView];
        
        // 加载模型数据
        MJApp *app = [[MJApp alloc] init];
        app.name = @"QQ";
        app.image = @"QQ";
        
        // 设置数据
        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 * _Nonnull change) {
        waekSelf.nameLabel.text = change[NSKeyValueChangeNewKey];
    }];
    
    [self.KVOController observe:viewModel keyPath:@"image" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary * _Nonnull change) {
        waekSelf.iconView.image = [UIImage imageNamed:change[NSKeyValueChangeNewKey]];
    }];
}

4、分层

界面层- 新闻列表,tableview
业务层- 加载展示数据,一些业务逻辑
数据层- 网络或本地

5、设计模式

比架构小,关注类与类之间的关系,主要有六大原则:依赖倒置、单一职责、最小知道、开闭、里氏替换、迪米特(邻近)

6、组件化

你可能感兴趣的:(iOS-架构设计)