【iOS】單例模式

單例模式在軟件開發中常常用到,在iOS系統framework也不少地方用到單例模式,例如 [NSUserDefaults standardUserDefaults], [NSBundle mainBundle]等,下面演示一下iOS如何實現單例模式ide

MRC模式spa

SingletonClass.h3d

#import <Foundation/Foundation.h>

@interface SingletonClass : NSObject

+ (SingletonClass *)sharedInstance;

@end

 

SingletonClass.mcode

#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模式blog

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.mit

#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

懶人模式 io

//單例頭宏(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
相關文章
相關標籤/搜索