iOS開源:AntNest - 吸收了 Go 語言的 Interface 模型的 iOS App 模塊化解耦編程的框架

lt4811 8年前發布 | 6K 次閱讀 iOS開發 移動開發

AntNest

簡介

AntNest 是吸收了 Go 語言的 Interface 模型的 iOS 的 App 模塊化解耦編程的框架。

  • 完全解耦的面向接口插件化模塊開發運行框架
  • 模塊具體實現與接口調用分離
  • 易擴展的模塊生命周期、事件分發

設計原則

  • Go 語言的 Interface 模型
  • 蟻巢的蟻室蟻道模型

基本架構

  • antRoom 為單獨的模塊
  • antChannel 為 antRoom 之間的通信通道

模塊的生命周期

目前支持的模塊的生命周期時間:

  • 基本的系統事件
  • 易擴展事件分發系統

基本的系統事件

目前的支持的基本的系統事件:

  • applicationDidEnterBackground
  • applicationWillEnterForeground
  • applicationDidFinishLaunchingWithOptions
  • applicationDidBecomeActive
  • applicationWillResignActive
  • applicationDidReceiveMemoryWarning
  • applicationWillTerminate
  • applicationSignificantTimeChange

在子模塊中實現對應的方法,AntNest 就會自動的分發到對應的模塊。

@implementation ANOrderAntRoom

ANT_EXPORT_ANTROOM()

  • (AntRoomLevel)antRoomLevel { return 1; }

  • (instancetype)createInstance:(NSDictionary *)launchOptions { return [ANOrderAntRoom new]; }

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { NSLog(@"ANOrderAntRoom room"); return YES; }

@end</code></pre>

擴展事件分發系統

AntNest 擴展事件分發是很方便的,舉個簡單的列子分發推送事件(AntNest 已經這個事件接口)

  • 定義事件接口
@protocol ANRemotePushEvent <NSObject>

  • (void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler ;

@end</code></pre>

  • 定義 AntNest 擴展實現接口,不用去實現具體的方法
@interface AntNest (ANRemotePushEvent)<ANRemotePushEvent>

@end

@implementation AntNest (ANRemotePushEvent)

@end</code></pre>

  • 觸發事件
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
  [[AntNest sharedAntNest] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

模塊

模塊注冊

ANT_EXPORT_ANTROOM()

模塊創建

實現 AntRoomProtocol 協議

antRoomLevel 表示模塊的初始化優先級

+ (AntRoomLevel)antRoomLevel {
  return 1;
}

  • (instancetype)createInstance:(NSDictionary )launchOptions { return [ANOrderAntRoom new]; }</code></pre>

    模塊通訊

    模塊間的通訊是通過 AntChannel 進行通訊,里面傳遞的都是實現 AntProtocol 協議對象。

    假如我們要獲取一個服務支持如下功能

    @property(nonatomic, strong) NSString orderID;

    @property(nonatomic, strong) NSString *customerName;

    @property(nonatomic, strong) NSString *shopName;

    • (void)payOrder;</code></pre>

      自定義一個 Protocol 獲取服務實例

      @protocol ANOrderDetailProtocol<AntProtocol>

    @property(nonatomic, strong) NSString *orderID;

    @property(nonatomic, strong) NSString *customerName;

    @property(nonatomic, strong) NSString *shopName;

    • (void)payOrder;

@end

...

id<ANOrderDetailProtocol> orderDetail = ANT_CHANNEL(ANOrderDetailProtocol, [[ANAntDes alloc] initWith:@"ANOrderDetailAnt"])</code></pre>

ant service 注冊

AntChannel 中傳遞的都是 ant service,

ANT_EXPORT_ANT()

  • (AntType)antType { return @"OrderDetailAnt"; }

  • (instancetype)createInstance:(id<ANOrderDetailDescriptionProtocol>)antDescription { ANOrderDetailViewController *order = [ANOrderDetailViewController new]; order.title = antDescription.orderID; order.customerName = antDescription.customerName; order.shopName = antDescription.shopName; return order; }</code></pre>

    Requirements

    • XCode

    Installation

    AntNest is available through CocoaPods . To install it, simply add the following line to your Podfile:

    pod "AntNest"

    Author

    qiang.shen@ele.me

    License

    AntNest is available under the MIT license. See the LICENSE file for more info.

     

     

 本文由用戶 lt4811 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!