beehives_beepropolis说明书

(15) 2024-10-05 18:01:01

1. 前言

BeeHive使用的核心有两个:Module和Service。

  • Module

用于响应ApplicationDelegate的各种事件。

  • Service

用于一个模块使用另一个模块的功能。

2. Module创建

创建一个基于BHModuleProtocol的对象即可。

@interface Module : NSObject <BHModuleProtocol> @end @implementation Module @end 

3. Module注册

  1. 使用宏BeeHiveMod

例如:

@BeeHiveMod(Module) 

该宏的定义:

#define BeeHiveMod(name) \ class BeeHive; char * k##name##_mod BeeHiveDATA(BeehiveMods) = ""#name""; 
  1. 使用宏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];} 
  1. 调用方法+ (void)registerDynamicModule:(Class)moduleClass;

例如:

+ (void)load { 
    [BeeHive registerDynamicModule:[self class]]; } 
  1. 配置文件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> 

beehives_beepropolis说明书 (https://mushiming.com/)  第1张

4. Module响应

实现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 { 
    } 

5. ServiceProtocol创建

创建一个基于BHModuleProtocol的协议。

@protocol ServiceProtocol <NSObject, BHServiceProtocol> @end 

6. Service创建

创建一个基于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]; } 

7. Service注册

  1. 使用宏BeeHiveService

例如:

@BeeHiveService(ServiceProtocol, Service) 

该宏的定义:

#define BeeHiveService(servicename,impl) \ class BeeHive; char * k##servicename##_service BeeHiveDATA(BeehiveServices) = "{ \""#servicename"\" : \""#impl"\"}"; 
  1. 调用方法- (void)registerService:(Protocol *)proto service:(Class)serviceClass;

例如:

[[BeeHive shareInstance] registerService:@protocol(ServiceProtocol) service:[Service class]]; 
  1. 配置文件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> 

beehives_beepropolis说明书 (https://mushiming.com/)  第2张

8. Service调用

调用方法- (id)createService:(Protocol *)proto;

例如:

id<ServiceProtocol> service = [[BeeHive shareInstance] createService:@protocol(ServiceProtocol)]; 

最后

BeeHive的运行原理,可从BeeHive原理解析中获取。
模块间互相调用服务时,非常依赖ServiceProtocol。
所以项目中使用BeeHive时,需要将各个模块的ServiceProtocol放置于一个基础模块,且各个模块需依赖该基础模块。

THE END

发表回复