Skip to content

Commit

Permalink
Merge pull request #74 from laichanwai/master
Browse files Browse the repository at this point in the history
更新单例 service 的实现逻辑
  • Loading branch information
SoXeon authored May 10, 2017
2 parents 29a648a + 9c7cc66 commit 74c900e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
25 changes: 14 additions & 11 deletions BeeHive/BHServiceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion BeeHive/BHServiceProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@optional

- (BOOL)singleton;
+ (BOOL)singleton;

+ (id)shareInstance;

Expand Down
3 changes: 1 addition & 2 deletions Example/BeeHive/BHUserTrackViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ @interface BHUserTrackViewController()<UserTrackServiceProtocol>

@implementation BHUserTrackViewController


-(BOOL)singleton
+(BOOL)singleton
{
return NO;
}
Expand Down

0 comments on commit 74c900e

Please sign in to comment.