AntNest 模块完全解耦方案

2017-03-09 | carlSQ | iOS

简介

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


@implementation AntNest (ANRemotePushEvent)

@end

复制代码
  • 触发事件
- (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;
}
复制代码

总结

欢迎 Star:github.com/carlSQ/AntN…

如有任何知识产权、版权问题或理论错误,还请指正。

转载请注明原作者及以上信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值