ios組件化三種方案

前言:

前段時(shí)間由于公司業(yè)務(wù)的需要,要求對(duì)項(xiàng)目進(jìn)行組件化的拆分,減少各個(gè)模塊之間耦合。希望達(dá)到的效果是各個(gè)組件能單獨(dú)的打私有pod,方便在其它工程里引用。由于項(xiàng)目現(xiàn)在比較龐大,目前只做了本地拆分。期間學(xué)習(xí)了很多有關(guān)組件化方面的知識(shí),現(xiàn)在做個(gè)總結(jié),后面會(huì)有有幾種方案相關(guān)的資料鏈接。先上我自己總結(jié)寫(xiě)的一個(gè)demo,github地址

概述:

組件化是為了各個(gè)模塊不進(jìn)行直接的相互引用,降低耦合度,那么如果組件A想調(diào)用組件B的時(shí)候要怎么辦呢,其實(shí)原理上都是通過(guò)中間件來(lái)調(diào)用,而不需要模塊間相互引用。 我們所看到的組件化方案,大體總結(jié)來(lái)說(shuō)有三種:
- 1.procotol方案
- 2.URL路由方案
- 3.target-action方案

一、procotol協(xié)議注冊(cè)方案

關(guān)于procotol協(xié)議注冊(cè)方案見(jiàn)到別人分享比較少,有次查資料的時(shí)候看到了,就研究了一下。
在demo中ProcotolManager作為中間件:

- (void)registServiceProvide:(id)provide forProcotol:(Protocol *)procotol;

- (id)serviceProvideForProcotol:(Protocol *)procotol;

所有組件對(duì)外提供的procotol和組件提供的服務(wù)由中間件統(tǒng)一管理,每個(gè)組件提供的procotol和服務(wù)是一一對(duì)應(yīng)的。

在ProductDetailServiceProvide中:load方法會(huì)應(yīng)用啟動(dòng)的時(shí)候調(diào)用,就會(huì)在ProcotolManager進(jìn)行注冊(cè)。ProductDetailServiceProvide遵守了ProductDetailServiceProcotol協(xié)議,所以對(duì)能外提供productDetailViewControllerWithProductId服務(wù)。

+ (void)load
{
    [[ProcotolManager sharedManger] registServiceProvide:[[self alloc] init] forProcotol:@protocol(ProductDetailServiceProcotol)];
}

- (UIViewController *)productDetailViewControllerWithProductId:(NSString *)productId
{
    ProcotolProductDetailViewController *detailVC = [[ProcotolProductDetailViewController alloc] init];
    detailVC.productId = productId;
    return detailVC;
}

所以在首頁(yè)中,通過(guò)ProcotolManager取出ProductDetailServiceProcotol對(duì)應(yīng)的服務(wù)提供者ProductDetailServiceProvide,就可以調(diào)用產(chǎn)品詳情中所提供的服務(wù),而不需要進(jìn)行直接引用。

二、URL路由方案

URL路由方案參考的是蘑菇街MGJRouter方案
蘑菇街 App 的組件化之路,講的比較詳細(xì)。

三、target-action方案

target-action方案是在學(xué)習(xí)CTMediator的基礎(chǔ)上進(jìn)行的,ZCMediator作為中間件,里面的實(shí)現(xiàn)也比較簡(jiǎn)單。

- (id)performTargetName:(NSString *)targetName actionName:(NSString *)actionName paramters:(NSDictionary *)paramtersDict;

執(zhí)行時(shí)查找對(duì)應(yīng)的target有沒(méi)有對(duì)外暴露的服務(wù),如果有則執(zhí)行。
主要的還是每一個(gè)組件暴露出的category,是對(duì)中間件的一個(gè)擴(kuò)展,調(diào)用每個(gè)組件對(duì)應(yīng)的category方法,然后在通過(guò)中間件調(diào)用對(duì)外暴露的服務(wù)。

#import "ZCMediator+ProductDetail.h"

//target
NSString *const MP_PRODUCT_DETAIL_TARGET = @"ProductDetailTarget";

//方法名
NSString *const MP_PRODUCT_DETAIL = @"productDetailViewControllerWithParameters";


@implementation ZCMediator (ProductDetail)

- (UIViewController *)productDetailViewControllerWithProductName:(NSString *)productName productId:(NSString *)productId
{
    if (!productName || !productId) {
        return nil;
    }
    return [self performTargetName:MP_PRODUCT_DETAIL_TARGET actionName:MP_PRODUCT_DETAIL paramters:@{@"productName":productName,@"productId":productId}];
}


@end

我們項(xiàng)目里使用的就是target-action方案。下次再寫(xiě)一篇幾種方案的比較吧。

參考鏈接:

iOS組件化方案

iOS應(yīng)用架構(gòu)談 組件化方案

蘑菇街 App 的組件化之路

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容