iOS开源:AntNest - 吸收了 Go 语言的 Interface 模型的 iOS App 模块化解耦编程的框架
lt4811
8年前
<h2>AntNest</h2> <h2>简介</h2> <p>AntNest 是吸收了 Go 语言的 Interface 模型的 iOS 的 App 模块化解耦编程的框架。</p> <ul> <li>完全解耦的面向接口插件化模块开发运行框架</li> <li>模块具体实现与接口调用分离</li> <li>易扩展的模块生命周期、事件分发</li> </ul> <p>设计原则</p> <ul> <li>Go 语言的 Interface 模型</li> <li>蚁巢的蚁室蚁道模型</li> </ul> <p style="text-align:center"><img src="https://simg.open-open.com/show/bb8aed7f6b8248eb082b36de0ebc0f49.png"></p> <p>基本架构</p> <ul> <li>antRoom 为单独的模块</li> <li>antChannel 为 antRoom 之间的通信通道</li> </ul> <p style="text-align:center"><img src="https://simg.open-open.com/show/780c4ecda53ede0a13e50ebf3128623e.png"></p> <h2>模块的生命周期</h2> <p>目前支持的模块的生命周期时间:</p> <ul> <li>基本的系统事件</li> <li>易扩展事件分发系统</li> </ul> <p>基本的系统事件</p> <p>目前的支持的基本的系统事件:</p> <ul> <li>applicationDidEnterBackground</li> <li>applicationWillEnterForeground</li> <li>applicationDidFinishLaunchingWithOptions</li> <li>applicationDidBecomeActive</li> <li>applicationWillResignActive</li> <li>applicationDidReceiveMemoryWarning</li> <li>applicationWillTerminate</li> <li>applicationSignificantTimeChange</li> </ul> <p>在子模块中实现对应的方法,AntNest 就会自动的分发到对应的模块。</p> <pre> <code class="language-objectivec">@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> <p>扩展事件分发系统</p> <p>AntNest 扩展事件分发是很方便的,举个简单的列子分发推送事件(AntNest 已经这个事件接口)</p> <ul> <li>定义事件接口</li> </ul> <pre> <code class="language-objectivec">@protocol ANRemotePushEvent <NSObject> - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler ; @end</code></pre> <ul> <li>定义 AntNest 扩展实现接口,不用去实现具体的方法</li> </ul> <pre> <code class="language-objectivec">@interface AntNest (ANRemotePushEvent)<ANRemotePushEvent> @end @implementation AntNest (ANRemotePushEvent) @end</code></pre> <ul> <li>触发事件</li> </ul> <pre> <code class="language-objectivec">- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { [[AntNest sharedAntNest] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; }</code></pre> <h2>模块</h2> <p>模块注册</p> <pre> <code class="language-objectivec">ANT_EXPORT_ANTROOM()</code></pre> <p>模块创建</p> <p>实现 AntRoomProtocol 协议</p> <p>antRoomLevel 表示模块的初始化优先级</p> <pre> <code class="language-objectivec">+ (AntRoomLevel)antRoomLevel { return 1; } + (instancetype)createInstance:(NSDictionary *)launchOptions { return [ANOrderAntRoom new]; }</code></pre> <h2>模块通讯</h2> <p>模块间的通讯是通过 AntChannel 进行通讯,里面传递的都是实现 AntProtocol 协议对象。</p> <p>假如我们要获取一个服务支持如下功能</p> <pre> <code class="language-objectivec">@property(nonatomic, strong) NSString *orderID; @property(nonatomic, strong) NSString *customerName; @property(nonatomic, strong) NSString *shopName; - (void)payOrder;</code></pre> <p>自定义一个 Protocol 获取服务实例</p> <pre> <code class="language-objectivec">@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> <p>ant service 注册</p> <p>AntChannel 中传递的都是 ant service,</p> <pre> <code class="language-objectivec">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> <h2>Requirements</h2> <ul> <li>XCode</li> </ul> <h2>Installation</h2> <p>AntNest is available through <a href="/misc/goto?guid=4958869288453138992" rel="nofollow,noindex">CocoaPods</a> . To install it, simply add the following line to your Podfile:</p> <pre> <code class="language-objectivec">pod "AntNest"</code></pre> <h2>Author</h2> <p><a href="/misc/goto?guid=4959741081405277726" rel="nofollow,noindex">qiang.shen@ele.me</a></p> <h2>License</h2> <p>AntNest is available under the MIT license. See the LICENSE file for more info.</p> <p> </p> <p> </p>