iOS分層架構(gòu)設(shè)計(jì)-實(shí)戰(zhàn)篇

一、前言

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ù)印,流程框圖如下:??????

主要邏輯流程框圖.png

基本步驟如下:

  • 用戶選擇服務(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)??????

目錄結(jié)構(gòu).png
  • 應(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)系我~
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,885評論 6 541
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,312評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,993評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,667評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,410評論 6 411
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,778評論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,775評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,955評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,521評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,266評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,468評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,998評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,696評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,095評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,385評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,193評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,431評論 2 378

推薦閱讀更多精彩內(nèi)容