우리는 정적 라이브러리 중 하나에 싱글 톤 클래스를 가지고 있습니다. 우리가 항상 그 상태를 "기억할"수 있기를 원하기 때문에 이것은 하나의 싱글 톤입니다. 본질적으로 그것은 사용자 관리 싱글 톤입니다.내 싱글 톤 클래스를 확장 가능하게 만드는 방법?
속성은 User *user
이고, 방법은 - (void)authenticateUser
입니다.
- (void)authenticateUser
메서드를 만들려는 클라이언트에게 전달하려고합니다. 이렇게하려면 UserManager
클래스를 확장하여 메서드를 재정의한다고 생각했습니다.
그러나 싱글 톤이므로 - (id)sharedInstance
및 맞춤 alloc
메서드가 있습니다. 그들 모두는 다음과 같다 :
static UserManager *_sharedInstance = nil;
+ (id)sharedInstance {
@synchronized([UserManager class]) {
if(!_sharedInstance){
id temp = [[self alloc] init];
[temp class];
}
return _sharedInstance;
}
return nil;
}
+ (id)alloc {
@synchronized([UserManager class]) {
NSAssert(_sharedInstance == nil, @"Attempted to allocate a second instance of a singleton");
_sharedInstance = [super alloc];
return _sharedInstance;
}
return nil;
}
, 그것은 서브 클래스 화해,이 UserManager
클래스를 확장 할 수 있습니다? 함수를 재정의하는 ChildUserManager
을 만들 수 있습니까? 아니면 "새"하위 클래스를 처리하기 위해 이러한 싱글 톤 메서드를 다시 작성해야합니까?
타사에서 쉽게이 클래스를 확장 할 수 있도록 이러한 방법을 수정할 수있는 방법이 있습니까?
감사
, 당신은 그것을 설정하는 사람을 위험이 있습니다. – Jano
아마도 static이 구현 안에 있기 때문에이 클래스 밖에서는 사용할 수 없습니까? –
죄송합니다. 컴파일 단위에서만 볼 수 있습니다. – Jano