Object-c单例模式

来源:互联网 发布:64位mips risc编程 编辑:程序博客网 时间:2024/05/01 03:07

#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) /

 /

static classname *shared##classname = nil; /

 /

+ (classname *)shared##classname /

{ /

    @synchronized(self) /

    { /

        if (shared##classname == nil) /

        { /

            shared##classname = [[self alloc] init]; /

        } /

    } /

     /

    return shared##classname; /

} /

 /

+ (id)allocWithZone:(NSZone *)zone /

{ /

    @synchronized(self) /

    { /

        if (shared##classname == nil) /

        { /

            shared##classname = [super allocWithZone:zone]; /

            return shared##classname; /

        } /

    } /

     /

    return nil; /

} /

 /

- (id)copyWithZone:(NSZone *)zone /

{ /

    return self; /

} /

 /

- (id)retain /

{ /

    return self; /

} /

 /

- (NSUInteger)retainCount /

{ /

    return NSUIntegerMax; /

} /

 /

- (void)release /

{ /

} /

 /

- (id)autorelease /

{ /

    return self; /

}

 

原创粉丝点击