设计模式与架构

何为架构?

架构(Architecture)
软件开发中的设计方案
类与类之间的关系、模块与模块之间的关系、客户端与服务端的关系

经常听到的架构名词
MVC、MVP、MVVM、VIPER、CDD
三层架构、四层架构
......

传统的MVC模式

Traditional MVC

苹果的 MVC

苹果官网地址

[Cocoa MVC]

示例:
Model

@interface ShopModel : NSObject
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *price;
@end

@implementation ShopModel
@end

DataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static  NSString *ID = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    ShopModel *shop = self.shopData[indexPath.row];
***************************************
cell和model之间并没有直接关系
***************************************
    cell.detailTextLabel.text = shop.price;
    cell.textLabel.text = shop.title;
    return cell;
}

优点:View、Model可以重复利用,可以独立使用
缺点:Controller的代码过于臃肿

MVC – 变种


示例:
Model

@interface TestModel : NSObject
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *imageName;
@end

@implementation TestModel
@end

View

@class TestModel;
@interface TestView : UIView
@property (strong, nonatomic) TestModel * model;
@end

@implementation TestView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        .....
    }
    return self;
}

- (void)setModel:(TestModel *)model{
    _model = model;
    self.iconView.image = [UIImage imageNamed:model.imageName];
    self.nameLabel.text = model.title;
}

@end

VC


- (void)viewDidLoad {
    [super viewDidLoad];
    TestModel *model = [[TestModel alloc] init];
    model.title = @"QQ";
    model.imageName = @"QQ";
    TestView *testView = [[TestView alloc] init];
    testView.model = model;
}

优点:对Controller进行瘦身,将View内部的细节封装起来了,外界不知道View内部的具体实现
缺点:View依赖于Model

MVP

Passive View variant of MVP

示例:
View

@interface TestView : UIView
- (void)setTitle:(NSString *)title andImageName:(NSString *)imageName;
@end

@implementation TestView
- (void)setTitle:(NSString *)title andImageName:(NSString *)imageName
{
    _iconView.image = [UIImage imageNamed:image];
    _nameLabel.text = title;
}
@end

VC

@interface ViewController ()
@property (strong, nonatomic) TestPresenter *presenter;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.presenter = [[TestPresenter alloc] initWithController:self];
}

@end

presenter

@interface TestPresenter : NSObject
- (instancetype)initWithController:(UIViewController *)controller;
@end

@implementation TestPresenter

- (instancetype)initWithController:(UIViewController *)controller
{
    if (self = [super init]) {
        self.controller = controller;
        
        // 创建View
        TestView *testView = [[TestView alloc] init];
        testView.frame = CGRectMake(100, 100, 100, 150);
        [controller.view addSubview:testView];
        
         // 加载模型数据
         TestModel *model = [[TestModel alloc] init];
         model.title = @"QQ";
         model.imageName = @"QQ";
        // 赋值数据
        [testView setName:model.title andImage:model.imageName];
    }
    return self;
}
@end

可以看到跟苹果的Apple’s MVC 很相似,多了一个 Presenter 模块,取代了Controller中转粘合剂的位置。Presenter本身与当前的ViewController中的生命周期并无关联,作为业务处理层,个人觉得称MVCP更合适

MVVM

MVVM

示例:
View

@class TestViewModel;
@interface TestView : UIView
@property (weak, nonatomic) TestViewModel *viewModel;
@end

@implementation TestView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        ....
    }
    return self;
}

- (void)setViewModel:(TestViewModel *)viewModel
{
    _viewModel = viewModel;
    __weak typeof(self) waekSelf = self;
    [self.KVOController observe:viewModel keyPath:@"title" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary * _Nonnull change) {
        waekSelf.nameLabel.text = change[NSKeyValueChangeNewKey];
    }];
    [self.KVOController observe:viewModel keyPath:@"imageName" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary * _Nonnull change) {
        waekSelf.iconView.image = [UIImage imageNamed:change[NSKeyValueChangeNewKey]];
    }];
}

VC

@interface ViewController ()
@property (strong, nonatomic) TestViewModel *viewModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewModel = [[TestViewModel alloc] initWithController:self];
}
@end

ViewModel

@interface TestViewModel : NSObject
- (instancetype)initWithController:(UIViewController *)controller;
@end

@interface TestViewModel() 
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *imageName;
@end

@implementation TestViewModel

- (instancetype)initWithController:(UIViewController *)controller
{
    if (self = [super init]) {
        // 创建View
        TestView *testView = [[TestView alloc] init];
        testView.frame = CGRectMake(100, 100, 100, 150);
        testView.viewModel = self;
        [controller.view addSubview:testView];
        // 加载模型数据
        TestModel *model = [[TestModel alloc] init];
         model.title = @"QQ";
         model.imageName = @"QQ";
        
        // 设置数据
        self.title = model.title;
        self.imageName = model.imageName;
    }
    return self;
}

在MVP的基础上,将P改成与V双向绑定的VM就变成了MVVM,个人觉得称为MVCVM更合适 View <-> C <-> ViewModel <-> Model
KVO监听属性是用的Facebook的开源框架 KVOController https://github.com/facebookarchive/KVOController

三层架构、四层架构


示例
Data

@interface HTTPTool : NSObject
+ (void)GET:(NSString *)URL params:(NSDictionary *)params success:(void (^)(id result))success failure:(void (^)(NSError *error))failure;
@end

@implementation HTTPTool
+ (void)GET:(NSString *)URL params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    // 调用AFN
}
@end
@interface DBTool : NSObject
@end
@implementation DBTool
@end
Service
@interface Service : NSObject
+ (void)loadData:(NSDictionary *)params success:(void (^)(NSArray *newsData))success failure:(void (^)(NSError *error))failure;
@end

@implementation Service

+ (void)loadData:(NSDictionary *)params success:(void (^)(NSArray *newsData))success failure:(void (^)(NSError *error))failure;
{
    // 先取出本地数据
//    [DBTool loadLocalData....];
    // 如果没有本地数据,就加载网络数据
//    [HTTPTool GET:@"xxxx" params:nil success:^(id result) {
//        success(array);
//    } failure:failure];
}
@end

设计模式

设计模式(Design Pattern)
是一套被反复使用、代码设计经验的总结
使用设计模式的好处是:可重用代码、让代码更容易被他人理解、保证代码可靠性
一般与编程语言无关,是一套比较成熟的编程思想

设计模式可以分为三大类

  • 创建型模式:对象实例化的模式,用于解耦对象的实例化过程
    单例模式、工厂方法模式,等等

  • 结构型模式:把类或对象结合在一起形成一个更大的结构
    代理模式、适配器模式、组合模式、装饰模式,等等

  • 行为型模式:类或对象之间如何交互,及划分责任和算法
    观察者模式、命令模式、责任链模式,等等

你可能感兴趣的:(设计模式与架构)