From 8b45197ee04d0dfe31ba426a6b05918b64888fea Mon Sep 17 00:00:00 2001 From: laizw Date: Tue, 9 May 2017 11:48:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8D=95=E4=BE=8B=20serv?= =?UTF-8?q?ice=20=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BeeHive/BHServiceManager.m | 25 ++++++++++++++----------- BeeHive/BHServiceProtocol.h | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) 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; From 9c7cc66a5dcf65ad9353a8fe4cfbb9118c151650 Mon Sep 17 00:00:00 2001 From: laizw Date: Tue, 9 May 2017 11:53:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20demo=20=E4=B8=AD?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=20service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example/BeeHive/BHUserTrackViewController.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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; }