Cocoa 中的单例设计模式

imhui posted @ 2012年8月06日 11:43 in Objective-C , 1484 阅读

static ShareManager *_shareManager = nil;

+ (ShareManager*)sharedManager
{
    @synchronized(self) {
        if (_shareManager == nil) {
            [[self alloc] init]; // assignment not done here
        }
    }
    return _shareManager;
}

+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (_shareManager == nil) {
            _shareManager = [super allocWithZone:zone];
            return _shareManager;  // assignment and return on first allocation
        }
    }
    return nil; //on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (unsigned)retainCount
{
    return UINT_MAX;  //denotes an object that cannot be released
}

- (oneway void)release {
    //do nothing
}


- (id)autorelease
{
    return self;
}

 

Avatar_small
seo service london 说:
2024年2月27日 18:16

Great, thanks for sharing this article post.Really thank you! Really Cool


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter