iOS 单例模式

来源:互联网 发布:贵金属数据下载 编辑:程序博客网 时间:2024/06/10 01:33


#import <Foundation/Foundation.h>


@interface Singleton :NSObject


+ (Singleton *)sharedSingleton;


+ (void)purgeSharedSingleton;


@end




#import "Singleton.h"


@implementation Singleton


staticSingleton* sInstance = nil;


+ (Singleton *)sharedSingleton

{

    @synchronized(self) {

        if (sInstance == nil) {

            [[selfalloc] init];

        }

    }

    returnsInstance;

}


+ (void)purgeSharedSingleton

{

    if (sInstance) {

        [sInstancerealRelease];

    }

}


+ (id)allocWithZone:(struct_NSZone *)zone

{

    @synchronized(self) {

        if (sInstance == nil) {

            sInstance = [superallocWithZone:zone];

            return sInstance;

        }

    }

    returnsInstance;

}


+ (id)copyWithZone:(struct_NSZone *)zone

{

    returnself;

}


- (id)copy

{

    returnself;

}


- (id)retain

{

    returnself;

}


- (NSUInteger)retainCount

{

    return UINT_MAX;

}


- (onewayvoid)release

{

    

}


- (id)autorelease

{

    returnself;

}


- (void)dealloc

{

    sInstance = nil;

    [superdealloc];

}


- (void)realRelease

{

    [superrelease];

}


@end


0 0
原创粉丝点击