单例模式在软件开发中经常用到,在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

相关文章: