diff --git a/BeeHive/BHServiceManager.m b/BeeHive/BHServiceManager.m index a53130e..a006bf3 100644 --- a/BeeHive/BHServiceManager.m +++ b/BeeHive/BHServiceManager.m @@ -86,23 +86,26 @@ - (id)createService:(Protocol *)service @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"%@ protocol does not been registed", NSStringFromProtocol(service)] userInfo:nil]; } - id protocolImpl = [[BHContext shareInstance] getServiceInstanceFromServiceName:NSStringFromProtocol(service)]; + NSString *serviceStr = NSStringFromProtocol(service); + id protocolImpl = [[BHContext shareInstance] getServiceInstanceFromServiceName:serviceStr]; if (protocolImpl) { return protocolImpl; } Class implClass = [self serviceImplClass:service]; - Method clzSingleMethod = class_getInstanceMethod(implClass, @selector(singleton)); - Method insSingleMethod = class_getClassMethod(implClass, @selector(singleton)); - if (clzSingleMethod == NULL && - insSingleMethod == NULL) { - return [implClass new]; + 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]; + + [[BHContext shareInstance] addServiceWithImplInstance:implInstance serviceName:serviceStr]; + return implInstance; + } } - - implInstance = [[implClass alloc] init]; - NSString *serviceStr = NSStringFromProtocol(service); - [[BHContext shareInstance] addServiceWithImplInstance:implInstance serviceName:serviceStr]; - return implInstance; + + return [[implClass alloc] init]; } #pragma mark - private diff --git a/BeeHive/BHServiceProtocol.h b/BeeHive/BHServiceProtocol.h index cfc9ca2..33f5a2b 100644 --- a/BeeHive/BHServiceProtocol.h +++ b/BeeHive/BHServiceProtocol.h @@ -12,7 +12,7 @@ @optional -- (BOOL)singleton; ++ (BOOL)singleton; + (id)shareInstance; diff --git a/Example/BeeHive/BHUserTrackViewController.m b/Example/BeeHive/BHUserTrackViewController.m index 1d3281f..1d07898 100644 --- a/Example/BeeHive/BHUserTrackViewController.m +++ b/Example/BeeHive/BHUserTrackViewController.m @@ -20,8 +20,7 @@ @interface BHUserTrackViewController() @implementation BHUserTrackViewController - --(BOOL)singleton ++(BOOL)singleton { return NO; }