工程項(xiàng)目中 Controller越來約臃腫,業(yè)務(wù)和View層耦合越來約嚴(yán)重,如何給Controller減減負(fù)迫在眉睫。
MVVM框架設(shè)計(jì)
如圖所示:

Controller: 里面只包含一個(gè)
View,由于是要將原來臃腫的Controller瘦身,這里的效果拔群。
Controller里做的主要是初始化這個(gè)View,設(shè)置Controller的title,以及默認(rèn)View的背景色。如果需要設(shè)置leftBarButtonItem或者rightBarButtonItem均在此處設(shè)置。View:此處包含所有視圖相關(guān)的View,所有
UI相關(guān)的操作放在這里,然后通過KVO監(jiān)聽ViewModel讓視圖變化。
ViewModel:
所有數(shù)據(jù)相關(guān)的屬性都存放在這里,同時(shí)用于被KVO的對(duì)象,傳統(tǒng)的ViewModel可以提供對(duì)數(shù)據(jù)以及業(yè)務(wù)的處理,但是為了防止ViewModel的臃腫,對(duì)ViewModel進(jìn)行進(jìn)一步拆分。Model: 網(wǎng)絡(luò)層相關(guān)model
DataProtocol:
處理數(shù)據(jù)的協(xié)議,在View或者Controller中被調(diào)用,效果是解耦。BusinessProtocol:
處理業(yè)務(wù)的協(xié)議,在View或者Controller中被調(diào)用,效果是解耦。DataHandler:處理數(shù)據(jù)的
實(shí)現(xiàn)。BusinessHandler:處理業(yè)務(wù)的
實(shí)現(xiàn)。
MVVM實(shí)現(xiàn)
Controller,View,ViewModel之間通過定義一個(gè)Content類進(jìn)行綁定操作。
Content類
#import <Foundation/Foundation.h>
#import "DDDDataHandler.h"
#import "DDDBusinessHandler.h"
@interface DDDContext : NSObject
@property (nonatomic, strong, readonly) DDDDataHandler *dataHandler;
@property (nonatomic, strong, readonly) DDDBusinessHandler *businessHandler;
- (instancetype)initWithDataHandler:(DDDDataHandler*)dataHandler
withBusinessHandler:(DDDBusinessHandler*)businessHandler;
@end
@implementation DDDContext
#pragma mark - lifecycle
- (instancetype)initWithDataHandler:(DDDDataHandler*)dataHandler
withBusinessHandler:(DDDBusinessHandler*)businessHandler
{
self = [super init];
if (self){
_dataHandler = dataHandler;
_dataHandler.context = self;
_businessHandler = businessHandler;
_businessHandler.context = self;
}
return self;
}
@end
同時(shí)給NSObject添加一個(gè)分類,使得他們可以調(diào)用Content
NSObject + DDD 類
#import <Foundation/Foundation.h>
@class DDDContext;
@interface NSObject (DDD)
@property (nonatomic, strong)DDDContext *context;
@end
DataHandler 類
#import <Foundation/Foundation.h>
#import "DDDContext.h"
@class DDDContext;
@interface DDDDataHandler : NSObject
@property (nonatomic, weak) DDDContext *context;
@end
BusinessHandler 類
#import <Foundation/Foundation.h>
#import "DDDContext.h"
@class DDDContext;
@interface DDDBusinessHandler : NSObject
@property (nonatomic, weak) DDDContext *context;
@property (nonatomic, weak) UIViewController *controller;
@end
只要在初始化Controller,View的時(shí)候,對(duì)Content進(jìn)行賦值,那么他們之間的關(guān)系就被綁定了。
例如,在Controller初始化中調(diào)用.
- (void)initContext
{
xxxDataHandler *datahandler = [xxxDataHandler new];
xxxBusinessHandler *businessHandler = [xxxBusinessHandler new];
businessHandler.controller = self;
self.context = [[DDDContext alloc] initWithDataHandler:datahandler withBusinessHandler:businessHandler];
}
== xxxBusinessHandler,xxxDataHandler為子類 ==
MVVM在工程中的應(yīng)用
使用本文中MVVM的項(xiàng)目,目錄結(jié)構(gòu)大致是這樣的

結(jié)果就是,原來臃腫的Controller被細(xì)分到了其他的類中,為其大大的減負(fù)。同時(shí),也做到了UI,業(yè)務(wù)分離,如果項(xiàng)目需要替換接口,業(yè)務(wù)邏輯,只需要修改對(duì)應(yīng)Handler即可,也起到了降低耦合的作用。
不過也有導(dǎo)致了一個(gè)Controller,對(duì)應(yīng)的相關(guān)類文件變多,創(chuàng)建麻煩的缺點(diǎn),因此,在新建類的時(shí)候,可以采用建立XCode模版,或者腳本實(shí)現(xiàn)自動(dòng)創(chuàng)建。本人采用的是執(zhí)行腳本創(chuàng)建,所以創(chuàng)建文件變多的麻煩并不存在。