一、前言
iOS的應(yīng)用一般情況下,無非就是用戶與界面間交互,形成操作指令,然后對數(shù)據(jù)進(jìn)行增刪改查。當(dāng)業(yè)務(wù)過于復(fù)雜時,我們就可以采用分層架構(gòu)設(shè)計(jì)。這樣可以最大程度上解耦,這里,我們主要介紹最經(jīng)典的三層架構(gòu)設(shè)計(jì)模型。大體上,分別為:應(yīng)用層、服務(wù)層、數(shù)據(jù)層。
Tips: 我們常用的MVC、MVP、MVVM等都是屬于應(yīng)用層內(nèi)的架構(gòu),也就是說,它們主要是為應(yīng)用層解耦等。而本文提到的分層架構(gòu)是針對整個項(xiàng)目而言。
二、案例分析
本篇主要以代碼實(shí)例為主,我們將通過一個簡單案例來展示分層架構(gòu)設(shè)計(jì)。
此例主要是模擬了打印機(jī)輸入端的業(yè)務(wù)邏輯。
我們的主要業(yè)務(wù)邏輯為打印和復(fù)印,流程框圖如下:??????
基本步驟如下:
用戶選擇服務(wù)類型:打印或者復(fù)印?
如果用戶選擇打印,則讓用戶輸入要打印的內(nèi)容,輸入完成后點(diǎn)擊提交。
如果用戶選擇復(fù)印,則讓用戶提供原件,操作完成后點(diǎn)擊提交。
提交后,系統(tǒng)生成打印輸出預(yù)覽,用戶確認(rèn)無誤后點(diǎn)擊立刻打印。
提交數(shù)據(jù)給打印機(jī),開始打印。
三、代碼實(shí)現(xiàn)
首先我們看一下目錄結(jié)構(gòu)??????
- 應(yīng)用層 - ApplicationLayer - MVC
數(shù)據(jù)模型 - Model
PrintModel.h
@property (nonatomic, copy) NSString *content;
視圖模型 - View
PrintView
CopyView
控制器 - Controller
MainController
PrintController
PreviewController
這一層就不多解釋了,采用里MVC架構(gòu),主要用于與用戶間的交互,接收操作指令等。
PrintController
- (void)configClient {
if (self.type == OptionPrint) {
[self configPrintClient];
} else if (self.type == OptionCopy) {
[self configCopyClient];
}
}
- (void)configPrintClient {
NSString *userInput = [self.printView getContent];
PrintModel *model = [[ServiceFactory sharedInstance].printService buildPrintModelWithContent:userInput];
NSLog(@"我要打印了 %@", model);
}
- (void)configCopyClient {
PrintModel *model1 = [[ServiceFactory sharedInstance].copyService getCopyPrintModel1];
PrintModel *model2 = [[ServiceFactory sharedInstance].copyService getCopyPrintModel2];
PrintModel *model3 = [[ServiceFactory sharedInstance].copyService getCopyPrintModel3];
NSLog(@"我要復(fù)印了 %@ \n %@ \n %@", model1, model2, model3);
}
- 服務(wù)層 - ServiceLayer
此層的所有服務(wù)由ServiceFactory來同一調(diào)配。ServiceFactory是一個單例,持有服務(wù)層的所有服務(wù)。由服務(wù)層來接觸數(shù)據(jù)層。應(yīng)用層則接觸服務(wù)層。
此處包含了兩個服務(wù):打印服務(wù)(PrintService)和復(fù)印服務(wù)(CopyService)
注:如果后續(xù)有新的服務(wù),則只需要在ServiceFactory中注冊一下即可。
ServiceFactory
#import <Foundation/Foundation.h>
#import "PrintService.h"
#import "CopyService.h"
@interface ServiceFactory : NSObject
+ (instancetype)sharedInstance;
@property (nonatomic, strong) PrintService *printService;
@property (nonatomic, strong) CopyService *copyService;
@end
#import "ServiceFactory.h"
@implementation ServiceFactory
+ (instancetype)sharedInstance {
static ServiceFactory *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [ServiceFactory new];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
[self configFoundation];
}
return self;
}
- (void)configFoundation {
self.printService = [PrintService new];
self.copyService = [CopyService new];
}
PrintService
#import <Foundation/Foundation.h>
@class PrintModel;
@interface PrintService : NSObject
- (PrintModel *)buildPrintModelWithContent:(NSString *)content;
@end
#import "PrintService.h"
#import "DataAccessFactory.h"
@implementation PrintService
- (PrintModel *)buildPrintModelWithContent:(NSString *)content {
PrintModel *model = [[DataAccessFactory sharedInstance].printDao buildPrintModelWithContent:content];
return model;
}
@end
CopyService
#import <Foundation/Foundation.h>
@class PrintModel;
@interface CopyService : NSObject
- (PrintModel *)getCopyPrintModel1;
- (PrintModel *)getCopyPrintModel2;
- (PrintModel *)getCopyPrintModel3;
@end
#import "CopyService.h"
#import "DataAccessFactory.h"
@implementation CopyService
- (PrintModel *)getCopyPrintModel1 {
PrintModel *model = [[DataAccessFactory sharedInstance].copyDao getCopyPrintModel1];
return model;
}
- (PrintModel *)getCopyPrintModel2 {
PrintModel *model = [[DataAccessFactory sharedInstance].copyDao getCopyPrintModel2];
return model;
}
- (PrintModel *)getCopyPrintModel3 {
PrintModel *model = [[DataAccessFactory sharedInstance].copyDao getCopyPrintModel3];
return model;
}
@end
- 數(shù)據(jù)層 - DataAccessLayer
此層由DataAccessFactory來同一調(diào)配。DataAccessFactory是一個單例,持有數(shù)據(jù)層的所有資源。由數(shù)據(jù)層來直接接觸數(shù)據(jù)。
此處包含了兩個數(shù)據(jù)資源:打印(PrintDAO)和復(fù)印(CopyDAO)
注:如果后續(xù)有新的數(shù)據(jù)層,則只需要在DataAccessFactory中注冊一下即可。
DataAccessFactory
#import <Foundation/Foundation.h>
#import "PrintDAO.h"
#import "CopyDAO.h"
@interface DataAccessFactory : NSObject
+ (instancetype)sharedInstance;
@property (nonatomic, strong) PrintDAO *printDao;
@property (nonatomic, strong) CopyDAO *copyDao;
@end
#import "DataAccessFactory.h"
@implementation DataAccessFactory
+ (instancetype)sharedInstance {
static DataAccessFactory *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [DataAccessFactory new];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
[self configFoundation];
}
return self;
}
- (void)configFoundation {
self.printDao = [PrintDAO new];
self.copyDao = [CopyDAO new];
}
@end
PrintDAO
#import <Foundation/Foundation.h>
@class PrintModel;
@interface PrintDAO : NSObject
- (PrintModel *)buildPrintModelWithContent:(NSString *)content;
@end
#import "PrintDAO.h"
#import "PrintModel.h"
@implementation PrintDAO
- (PrintModel *)buildPrintModelWithContent:(NSString *)content {
PrintModel *model = [[PrintModel alloc] init];
model.content = content;
return model;
}
@end
CopyDAO
#import <Foundation/Foundation.h>
@class PrintModel;
@interface CopyDAO : NSObject
- (PrintModel *)getCopyPrintModel1;
- (PrintModel *)getCopyPrintModel2;
- (PrintModel *)getCopyPrintModel3;
@end
#import "CopyDAO.h"
#import "PrintModel.h"
@implementation CopyDAO
- (PrintModel *)getCopyPrintModel1 {
PrintModel *model = [[PrintModel alloc] init];
model.content = @"Hello world";
return model;
}
- (PrintModel *)getCopyPrintModel2 {
PrintModel *model = [[PrintModel alloc] init];
model.content = @"Where there is a will, there is a way";
return model;
}
- (PrintModel *)getCopyPrintModel3 {
PrintModel *model = [[PrintModel alloc] init];
model.content = @"We will, we will rock you";
return model;
}
@end
四、結(jié)論分析
分層架構(gòu)設(shè)計(jì)最明顯的特點(diǎn)就是,結(jié)構(gòu)分層,且不可跨層訪問,比如,應(yīng)用層不能直接訪問數(shù)據(jù),僅有數(shù)據(jù)層能直接操作數(shù)據(jù),也就是說,應(yīng)用層必須先經(jīng)由服務(wù)層訪問數(shù)據(jù)層,再由數(shù)據(jù)層操作數(shù)據(jù)。這樣有人就要問了,為什么要這么復(fù)雜?在應(yīng)用層導(dǎo)入PrintModel.h,直接操作不就行了?
當(dāng)然是行的,一樣可以達(dá)到效果,但是,如果業(yè)務(wù)邏輯發(fā)生變動或新增邏輯,我們就必須修改或重寫應(yīng)用層,這是我們最不愿看到的,再或者,另一個模塊也要用到類似的業(yè)務(wù)邏輯,比如有了一臺高級打印機(jī),在打印和復(fù)印功能上是一樣的,那我們是不是要再復(fù)制一份普通打印機(jī)應(yīng)用層的邏輯呢?這些都是很嚴(yán)重的問題。
從這里,我們不難看出,首先我們是將可見與不可見分層,再將公用的形成模塊沉底,使每層間能相互獨(dú)立,有清晰的依賴關(guān)系,這就是分層架構(gòu)的目的。
五、寫在最后
這樣一個簡單的案例肯定不能完成的覆蓋分層架構(gòu),但已經(jīng)明確的體現(xiàn)了分層架構(gòu)的核心,在實(shí)際開發(fā)中,我們要根據(jù)實(shí)際的業(yè)務(wù)邏輯來定義服務(wù)層和數(shù)據(jù)層,應(yīng)用層基本通用。看似簡單,但需要大量的實(shí)戰(zhàn)經(jīng)驗(yàn)才能完美吸收,也希望讀者能多動手。
- 有需要源碼的可以聯(lián)系我~