設計模式與架構

何為架構?

架構(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<NSKeyValueChangeKey,id> * _Nonnull change) {
        waekSelf.nameLabel.text = change[NSKeyValueChangeNewKey];
    }];
    [self.KVOController observe:viewModel keyPath:@"imageName" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _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() <TestViewDelegate>
@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)
是一套被反復使用、代碼設計經驗的總結
使用設計模式的好處是:可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性
一般與編程語言無關,是一套比較成熟的編程思想

設計模式可以分為三大類

  • 創建型模式:對象實例化的模式,用于解耦對象的實例化過程
    單例模式、工廠方法模式,等等

  • 結構型模式:把類或對象結合在一起形成一個更大的結構
    代理模式、適配器模式、組合模式、裝飾模式,等等

  • 行為型模式:類或對象之間如何交互,及劃分責任和算法
    觀察者模式、命令模式、責任鏈模式,等等

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。