BeeHive使用的核心有两个:Module和Service。
用于响应ApplicationDelegate的各种事件。
用于一个模块使用另一个模块的功能。
创建一个基于BHModuleProtocol
的对象即可。
@interface Module : NSObject <BHModuleProtocol> @end @implementation Module @end
BeeHiveMod
例如:
@BeeHiveMod(Module)
该宏的定义:
#define BeeHiveMod(name) \ class BeeHive; char * k##name##_mod BeeHiveDATA(BeehiveMods) = ""#name"";
BH_EXPORT_MODULE
例如:
BH_EXPORT_MODULE(NO)
该宏的定义:
#define BH_EXPORT_MODULE(isAsync) \ + (void)load { [BeeHive registerDynamicModule:[self class]]; } \ -(BOOL)async { return [[NSString stringWithUTF8String:#isAsync] boolValue];}
+ (void)registerDynamicModule:(Class)moduleClass;
例如:
+ (void)load {
[BeeHive registerDynamicModule:[self class]]; }
BeeHive.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>URLGlobalScheme</key> <string>com.alibaba.beehive</string> <key>moduleClasses</key> <array> <dict> <key>moduleClass</key> <string>HomeModule</string> <key>moduleLevel</key> <integer>1</integer> <key>modulePriority</key> <string>600</string> </dict> <dict> <key>moduleClass</key> <string>TMTradeAdapter</string> <key>moduleLevel</key> <integer>1</integer> <key>modulePriority</key> <string>599</string> </dict> </array> </dict> </plist>
实现BHModuleProtocol
的方法,例如:
- (id)init{
if (self = [super init]) {
NSLog(@"Module init"); } return self; } -(void)modInit:(BHContext *)context {
NSLog(@"模块初始化中"); NSLog(@"%@",context.moduleConfigName); id<ServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(ServiceProtocol)]; service.itemId = @"我是单例"; } - (void)modSetUp:(BHContext *)context {
[[BeeHive shareInstance] registerService:@protocol(ServiceProtocol) service:[ServiceClass class]]; NSLog(@"Module setup"); } - (void)basicModuleLevel {
}
创建一个基于BHModuleProtocol
的协议。
@protocol ServiceProtocol <NSObject, BHServiceProtocol> @end
创建一个基于ServiceProtocol的对象即可。
@interface Service : NSObject <ServiceProtocol> @end @implementation Service @end
BHServiceProtocol
支持单例和多例:
Service对象实现方法+ (BOOL)singleton;
和+ (id)shareInstance;
+ (BOOL)singleton {
return YES; } + (id)shareInstance {
static dispatch_once_t p; static id instance = nil; dispatch_once(&p, ^{
instance = [[self alloc] init]; }); return instance; }
方法+ (BOOL)singleton;
返回YES,BHServiceManager
会调用Service类的方法+ (id)shareInstance;
。
Service对象实现方法+ (BOOL)singleton;
和+ (id)shareInstance;
+ (BOOL)singleton {
return NO; }
方法+ (BOOL)singleton;
返回NO,BHServiceManager
会调用Service对象的方法- (instancetype)init;
。
具体逻辑可参考BHServiceManager
的以下代码
- (id)createService:(Protocol *)service withServiceName:(NSString *)serviceName shouldCache:(BOOL)shouldCache {
if (!serviceName.length) {
serviceName = NSStringFromProtocol(service); } id implInstance = nil; if (![self checkValidService:service]) {
if (self.enableException) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"%@ protocol does not been registed", NSStringFromProtocol(service)] userInfo:nil]; } } NSString *serviceStr = serviceName; if (shouldCache) {
id protocolImpl = [[BHContext shareInstance] getServiceInstanceFromServiceName:serviceStr]; if (protocolImpl) {
return protocolImpl; } } Class implClass = [self serviceImplClass:service]; if ([[implClass class] respondsToSelector:@selector(singleton)]) {
if ([[implClass class] singleton]) {
if ([[implClass class] respondsToSelector:@selector(shareInstance)]) implInstance = [[implClass class] shareInstance]; else implInstance = [[implClass alloc] init]; if (shouldCache) {
[[BHContext shareInstance] addServiceWithImplInstance:implInstance serviceName:serviceStr]; return implInstance; } else {
return implInstance; } } } return [[implClass alloc] init]; }
BeeHiveService
例如:
@BeeHiveService(ServiceProtocol, Service)
该宏的定义:
#define BeeHiveService(servicename,impl) \ class BeeHive; char * k##servicename##_service BeeHiveDATA(BeehiveServices) = "{ \""#servicename"\" : \""#impl"\"}";
- (void)registerService:(Protocol *)proto service:(Class)serviceClass;
例如:
[[BeeHive shareInstance] registerService:@protocol(ServiceProtocol) service:[Service class]];
BHService.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>service</key> <string>UserTrackServiceProtocol</string> <key>impl</key> <string>BHUserTrackViewController</string> </dict> </array> </plist>
调用方法- (id)createService:(Protocol *)proto;
例如:
id<ServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(ServiceProtocol)];
BeeHive的运行原理,可从BeeHive原理解析中获取。
模块间互相调用服务时,非常依赖ServiceProtocol。
所以项目中使用BeeHive时,需要将各个模块的ServiceProtocol放置于一个基础模块,且各个模块需依赖该基础模块。