AntNest 0.5.0

AntNest 0.5.0

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2017年6月

carlSQ 维护。



AntNest 0.5.0

  • 作者
  • carlSQ

简介

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

扩展事件分发系统

AntNest 扩展事件分发非常方便,以下是一个简单的例子:分发推送事件(AntNest 已经具备此事件接口)

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

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

@end
  • 定义 AntNest 扩展开实现接口,无需实现具体方法
@interface AntNest (ANRemotePushEvent)<ANRemotePushEvent>

@end
  • 注册事件接口
[[AntNest sharedAntNest] registerProtocolEvent:@protocol(ANRemotePushEvent)];
  • 触发事件
- (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];
}

模块通讯

模块间通讯通过 AntChannel 进行,传递的是实现 AntProtocol 协议的对象。

如果我们想获取一个支持以下功能的服务

  @property(nonatomic, strong) NSString *orderID;

  @property(nonatomic, strong) NSString *customerName;

  @property(nonatomic, strong) NSString *shopName;

  - (void)payOrder;

自定义 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"])

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;
}

要求

  • XCode

安装

通过 CocoaPods(https://cocoapods.org.cn)可以获取 AntNest。要安装,只需将以下行添加到 Podfile 中

pod "AntNest"

作者

[email protected]

许可

AntNest采用MIT许可证。有关更多信息,请参阅LICENSE文件。