IOS APP组件化的基础是私用pod创建私有库,如果不清楚这方面内容的,可以参看:https://blog.csdn.net/dingpwen/article/details/114878354
IOS有多种第三方库来辅助实现组件化开发,其中最解耦又最简单的就是CTMediator。对比Android,由于IOS编程语言的特性(或其编译器的特性),CTMediator更加简单又更加彻底地实现了模块功能的独立。
1. CTMediator简介
CTMediator 实现组件化,从软件架构上来说,就是一种中间件架构。
网上有很多描叙CTMediator的文章,这里只说下重点。CTMediator主要作用在于协助模块的对外访问接口,相对于通常的模块的开发,使用CTMediator架构,它需要添加两类独特的代码。
1)在模块内,对于需要添加Target_ModuleA之类的class,而其方法必须是Action_funcA之类命名,且参数有且只有一个NSDictionary,例如:
@interface Target_ModuleA : NSObject
- (UIViewController*)Action_FuncA:(NSDictionary*)params;
@end
注意,这里的ModuleA和FuncA名字任意,可以完全同项目中的类名字不一致,只是下面要用到。
2)需要定义一个CTMediator的扩展类,以便给其它模块访问,例如:
@interface CTMediator (ModuleA)
- (UIViewController*)ModuleA_FuncA;
@end
这里的类名接口名任意,没有什么特殊要求,只是其实现部分要注意与1)部分的类名接口名一致:
@implementation CTMediator (ModuleA)
- (UIViewController*)ModuleA_FuncA{
return [self performTarget:@"ModuleA" action:@"funcA" params:nil shouldCacheTarget:NO]<