【iOS】单例模式

单例模式在软件开发中经常用到,在iOS系统framework也很多地方用到单例模式,例如 [NSUserDefaults standardUserDefaults], [NSBundle mainBundle]等,下面演示一下iOS如何实现单例模式

MRC模式

SingletonClass.h

#import <Foundation/Foundation.h>

@interface SingletonClass : NSObject

+ (SingletonClass *)sharedInstance;

@end

 

SingletonClass.m

#import "SingletonClass.h"

@implementation SingletonClass

static SingletonClass *_singletonInstance = nil;                     
+ (instancetype)sharedInstance{                                 
    @synchronized(self){                                        
        if (!_singletonInstance) {                              
            _singletonInstance = [[self alloc] init];           
        }                                                       
    }                                                           
    return _singletonInstance;                                  
}                                                               

+ (id)allocWithZone:(NSZone *)zone{                             
    @synchronized(self){                                        
        if (!_singletonInstance) {                              
            _singletonInstance = [super allocWithZone:zone];    
        }                                                       
        return _singletonInstance;                              
    }                                                           
    return nil;                                                 
}                                                               

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

- (instancetype)retain                                          
{                                                               
    return self;                                                
}                                                               

- (unsigned)retainCount                                         
{                                                               
    return UINT_MAX;                                            
}                                                               

- (instancetype)autorelease                                     
{                                                               
    return self;                                                
}                                                               

- (oneway void)release                                          
{                                                               
}                                                               

@end

懒人技巧:把单例的定义与实现定义成宏

//单例头宏
#define DEFINE_SINGLETON_HEADER(className)  \
    + (className *)sharedInstance;          \

//单例实现宏
#define DEFINE_SINGLETON_IMPLEMENTATION(className)              \
static className *_singletonInstance = nil;                     \
+ (instancetype)sharedInstance{                                 \
    @synchronized(self){                                        \
        if (!_singletonInstance) {                              \
            _singletonInstance = [[self alloc] init];           \
        }                                                       \
    }                                                           \
    return _singletonInstance;                                  \
}                                                               \
                                                                \
+ (id)allocWithZone:(NSZone *)zone{                             \
    @synchronized(self){                                        \
        if (!_singletonInstance) {                              \
            _singletonInstance = [super allocWithZone:zone];    \
        }                                                       \
        return _singletonInstance;                              \
    }                                                           \
    return nil;                                                 \
}                                                               \
                                                                \
- (instancetype)copyWithZone:(NSZone *)zone;                    \
{                                                               \
    return self;                                                \
}                                                               \
                                                                \
- (instancetype)retain                                          \
{                                                               \
    return self;                                                \
}                                                               \
                                                                \
- (unsigned)retainCount                                         \
{                                                               \
    return UINT_MAX;                                            \
}                                                               \
                                                                \
- (instancetype)autorelease                                     \
{                                                               \
    return self;                                                \
}                                                               \
                                                                \
- (oneway void)release                                          \
{                                                               \
}                                                               \
SingletonDefine
#import <Foundation/Foundation.h>
#import "SingletonDefine.h"

@interface SingletonClass : NSObject

DEFINE_SINGLETON_HEADER(SingletonClass)

@end
SingletonClass.h
#import "SingletonClass.h"

@implementation SingletonClass

DEFINE_SINGLETON_IMPLEMENTATION(SingletonClass)                                                        

@end
SingletonClass.m

 

ARC模式

SingletonClass.h

#import <Foundation/Foundation.h>

@interface SingletonClass : NSObject

+ (instancetype)sharedInstance;

//禁用alloc,init,new 创建对象,否则编译会报错
+(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));
-(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
+(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));

@end

SingletonClass.m

#import "SingletonClass.h"

@implementation SingletonClass

+(instancetype) sharedInstance {                            
    static dispatch_once_t predicate;                       
    static SingletonClass *instance = nil;
    dispatch_once(&predicate, ^{                            
        instance = [[super alloc] initUniqueInstance];      
    });                                                     
    return instance;                                        
}                                                           

-(instancetype) initUniqueInstance {                        
    return [super init];                                    
}                                                           

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

@end

懒人模式 

//单例头宏(ARC)
#define DEFINE_SINGLETON_HEADER(className)                  \
+ (instancetype)sharedInstance;                             \


//单例实现宏(ARC)
#define DEFINE_SINGLETON_IMPLEMENTATION(className)                  \
+(instancetype) sharedInstance {                                    \
    static dispatch_once_t predicate;                               \
    static className *_singletonInstance = nil;                     \
    dispatch_once(&predicate, ^{                                    \
        _singletonInstance = [[super alloc] init];                  \
    });                                                             \
    return _singletonInstance;                                      \
}                                                                   \
                                                                    \
- (instancetype)copyWithZone:(NSZone *)zone                         \
{                                                                   \
    return self;                                                    \
}                                                                   \
SingletonDefine.h
#import <Foundation/Foundation.h>
#import "SingletonDefine.h"

@interface SingletonClass : NSObject

DEFINE_SINGLETON_HEADER(SingletonClass)

@end
SingletonClass.h
#import "SingletonClass.h"

@implementation SingletonClass

DEFINE_SINGLETON_IMPLEMENTATION(SingletonClass)

@end
SingletonClass.m

 

posted @ 2015-05-14 21:32  bomo  阅读(1459)  评论(9编辑  收藏  举报