AntNest 模块完全解耦方案

作者: carlSQ | 来源:发表于2017-03-09 13:59 被阅读0次

简介

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"])

AntRoom中的 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;
}

总结

欢迎PR

相关文章

  • AntNest 模块完全解耦方案

    简介 AntNest 是吸收了 Go 语言的 Interface 模型的 iOS 的 App 模块化解耦编程的框架...

  • 模块解耦之快速编译

    一快速编译 1.背景 为了实现各模块之家的解耦和各模块和App之间的快速可插拔要求,设计了此方案。 此方案能够解决...

  • 模块化与解耦

    [模块化与解耦](模块化与解耦 - 刘坤的技术博客[https://blog.cnbluebox.com/blog...

  • 青团社拆包依赖-QDepend

    青团社业务发展愈发多样,Android使用模块化设计进行业务的解耦,在代码都解耦上我们已经有一套方案,但在工程的模...

  • iOS三方框架之 - AFNetworking的Https认证流

    对AFNetworking进行解耦AFNetworking解耦后可以分为以下几个模块:1. NSURLSessio...

  • iOS 组件化(一)

    组件化 组件化就是将模块单独抽离,分层,通过制定的通讯方式,实现解耦 组件化优点 模块间的解耦 模块重用 提交团队...

  • ios组件化/模块化

    1.博客文章: [模块化与解耦](模块化与解耦 - 刘坤的技术博客) [浅析 iOS 应用组件化设计](Skyli...

  • Google MVP Demo学习心得

    MVP模式通过presenter隔离了View和Model两个模块之间的交互,实现View和Model完全解耦。 ...

  • iOS组件化/模块化 APP方案实践篇

    1.博客文章: [模块化与解耦](模块化与解耦 - 刘坤的技术博客) 浅析 iOS 应用组件化设计 [iOS组件化...

  • 项目笔记之 重构

    业务逻辑梳理功能模块梳理模块抽象解耦封装->模块组件化模块内部深度优化

网友评论

    本文标题:AntNest 模块完全解耦方案

    本文链接:https://www.haomeiwen.com/subject/zwlwgttx.html