• 【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
  • 相关阅读:
    软件测试工程师的素质
    软件测试阶段的划分
    如何去涉及测试用例
    如何判断测试结束
    Linux常用命令大全
    测试用例设计方法
    Web测试方法
    loadrunner
    谈谈重复性测试
    软件测试思维导图
  • 原文地址:https://www.cnblogs.com/bomo/p/4504520.html
Copyright © 2020-2023  润新知