我如何確保用戶不要調用init,而是客戶端應該調用sharedSingleton來獲取共享實例。objc中的單例模式,如何保持init私有?
@synthesize delegate;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
+ (LoginController *)sharedSingleton
{
static LoginController *sharedSingleton;
@synchronized(self)
{
if (!sharedSingleton)
sharedSingleton = [[LoginController alloc] init];
CdtMiscRegisterConnectionChangeListenerObjc(test_ConnectionChangeListenerCallback);
return sharedSingleton;
}
}
它們可與一種擴展(又名「空類」),雖然效仿,但我認爲它不會隱藏繼承的初始化。 –