Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 BHServiceProtocol 单例的疑问 #71

Closed
laizhenwei opened this issue May 4, 2017 · 5 comments
Closed

关于 BHServiceProtocol 单例的疑问 #71

laizhenwei opened this issue May 4, 2017 · 5 comments

Comments

@laizhenwei
Copy link
Contributor

BHServiceProtocol 中定义了shareInstance这个方法,那么我通过 createService: 获取一个单例对象的时候,为什么单例对象不是通过我实现的方法创建?

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];
}

implInstance = [[implClass alloc] init];
NSString *serviceStr = NSStringFromProtocol(service);
[[BHContext shareInstance] addServiceWithImplInstance:implInstance serviceName:serviceStr];
return implInstance;

在上面 BHServiceManagercreateService: 实现中,只是判断了是否实现了 singleton 方法,然后是通过 [[implClass alloc] init] 创建的单例。

@SoXeon
Copy link
Contributor

SoXeon commented May 5, 2017

这里是为了便于统一,自己实现的创建方法是有入参吗?

@waterfly
Copy link

waterfly commented May 8, 2017

有些类是需要自己创建的单例,感觉判断下是否实现 + (id)shareInstance 会更好

@laizhenwei
Copy link
Contributor Author

@SoXeon 我的疑问是,你定义了创建单例的方法,我遵循了你的方法返回一个单例,可为什么你没有使用?

@SoXeon
Copy link
Contributor

SoXeon commented May 10, 2017

implInstance = [[implClass alloc] init];
这边的确不太合理,如果是一个单例Service的话直接调类方法就好,但方法名不一致的话,不好判断
init先兜底,这样可以保证调用一致性,并且BeeHive去维护这样一份ServiceInstances,这样不过比较重,在考虑分治的改造想法😄

@SoXeon
Copy link
Contributor

SoXeon commented May 10, 2017

已合并

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants