iOS单利

作者: 代江波 | 来源:发表于2019-07-31 11:29 被阅读0次
    ARC环境
    懒汉式

    .h文件

    @interface SingletonTool : NSObject
    
    + (instancetype)shareSingletonTool;
    
    @end
    
    

    .m文件

    #import "SingletonTool.h"
    
    @implementation SingletonTool
    //必须使用static 否则会被其他类使用extern引用修改
    static id _instance;
    
    /**
     对外共享一个类方法,方便其他地方使用
     */
    + (instancetype)shareSingletonTool{
        if (_instance == nil) {//防止频繁加锁操作
            @synchronized (self) {//加锁是为了避免多线程同时使用
                if (_instance == nil) {
                    _instance = [[self alloc] init];
                }
            }
        }
        return _instance;
    }
    
    /**
     重写此方法,避免开辟多个内存空间
     */
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        if (_instance == nil) {
            @synchronized (self) {
                if (_instance == nil) {
                    _instance = [super allocWithZone:zone];
                }
            }
        }
        return _instance;
    }
    
    /**
     重写copy方法,避免copy时产生新的对象
     */
    - (id)copyWithZone:(NSZone *)zone{
        //直接return是因为copy时,_instance已经有值了
        return _instance;
    }
    @end
    
    
    饿汉式

    饿汉式是让程序加载所有资源时就保证这个单利存在内存之中,所以此时不必考虑多线程的问题

    @implementation SingletonTool
    static id _instance;
    
    //此方法是类被加载的第一次时调用,并且只会调用一次
    + (void)load{
        _instance = [[self alloc] init];
    }
    
    + (instancetype)shareSingletonTool{
        return _instance;
    }
    
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
        return _instance;
    }
    
    - (id)copyWithZone:(NSZone *)zone{
        return _instance;
    }
    
    @end
    
    使用GCD
    + (instancetype)shareSingletonTool{
        static dispatch_once_t onceToken;
        //此方法的代码块的任务在程序运行到结束的过程中只运行一次,并且时线程安全的
        dispatch_once(&onceToken, ^{
            _instance = [[self alloc] init];
        });
        return _instance;
    }
    
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super allocWithZone:zone];
        });
        return _instance;
    }
    
    MRC环境

    MRC环境多了处理release retain retainCount autorelease这四个方法

    //release什么都不做,避免引用计数为0,对象被销毁
    - (oneway void)release {
     }
    
    //retain直接返回self
    - (id)retain { 
        return self;
     }
    
    //retainCount始终保持引用计数为1
    - (NSUInteger)retainCount { 
        return 1;
    }
    
    - (id)autorelease { 
        return self;
    }
    
    单利宏

    创建一个单利头文件,将单利实现代码封住成一个宏即可

    // .h文件
    #define HMSingletonH(name) + (instancetype)shared##name;
    
    // .m文件
    #if __has_feature(objc_arc)
    
    #define HMSingletonM(name) \
    static id _instace; \
    \
    + (id)allocWithZone:(struct _NSZone *)zone { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instace = [super allocWithZone:zone]; \
        }); \
        return _instace; \
    } \
    \
    + (instancetype)shared##name { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instace = [[self alloc] init]; \
        }); \
        return _instace; \
    } \
    \
    - (id)copyWithZone:(NSZone *)zone { \
        return _instace; \
    }
    
    #else
    
    #define HMSingletonM(name) \
    static id _instace; \
    \
    + (id)allocWithZone:(struct _NSZone *)zone { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
           _instace = [super allocWithZone:zone]; \
        }); \
        return _instace; \
    } \
    \
    + (instancetype)shared##name { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
           _instace = [[self alloc] init]; \
        }); \
        return _instace; \
    } \
    \
    - (id)copyWithZone:(NSZone *)zone { \
        return _instace; \
    } \
    \
    - (oneway void)release { } \
    - (id)retain { return self; } \
    - (NSUInteger)retainCount { return 1;} \
    - (id)autorelease { return self;}
    
    #endif
    
    

    相关文章

      网友评论

          本文标题:iOS单利

          本文链接:https://www.haomeiwen.com/subject/zmanrctx.html