七. 单例模式

一. ARC环境下的单例模式

  1. 单例模式的基本概念

    • 单例, 顾名思义, 即在整个程序中, 某一个类只有唯一一个实例, 他贯穿整个应用程序, 不会被创建第二次, 分配第二次内存.
    • 对于单例, 较多的都是应用于一些工具类, 可以起到特定作用并且功能比较常用的类, 就可以制作为单例模式.
  2. 在ARC环境下的单例模式

    1. 在类的内部提供一个static修饰的全局变量

      • 使用static修饰的变量, 它的生命周期会延长为整个程序结束的时候才被销毁
      • 并且这个变量只会生成一份内存, 只会初始化一次
    2. 提供一个类方法, 方便外界访问

      • 这一点建议遵循苹果的命名习惯: shareXXXX
    3. 重写+allocWithZone方法, 保证永远都只为单例对象分配一次内存空间

    4. 为了严谨, 可以重写-copyWithZone-mutableCopyWithZone方法

    5. 单例代码有两种不同的方式

      • GCD的一次性代码: dispatch_once
        • 使用的比较多, 保证block中的代码, 在整个程序中只运行一次
        • 由于单例对象是一个静态变量, 程序结束才释放, 所以使用一次性代码初始化后, 他会一直存在, 并通过返回单例对象的方法随时可以获取
      • GCD线程锁: @synchronized
        • 此方法常用于多线程, 并且这个单例对象会频繁被子线程使用的时候调用
        • 建议使用一次性代码的单例创建放放
    6. 代码示例:

       // 1. 创建静态变量
       static FHTool * _instance;
       
       // 2. 重写allocWithZone方法,确保内存值分配一次并且每次返回的都是同一个实例
       //+ (instancetype)allocWithZone:(struct _NSZone *)zone {
       //    
       //    // 2.1 为了防止多个线程同时使用该方法,避免访问同一块内存出现问题,因此要加上互斥锁
       //    @synchronized(self) {
       //        if (_instance == nil) {
       //            _instance = [super allocWithZone:zone];
       //        }
       //    }
       //    return _instance;
       //}
       
       // 3. 使用GCD一次性代码
       + (instancetype)allocWithZone:(struct _NSZone *)zone {
           
           // 3.1 一次性代码保证全局只执行一次, 之后永远返回静态变量_instance
           static dispatch_once_t onceToken;
           dispatch_once(&onceToken, ^{
               _instance = [super allocWithZone:zone];
           });
           
           return _instance;
       }
       
       + (instancetype)shareFHTool {
           // alloc方法会在底层自动调用allocWithZone方法
           return [[self alloc] init];
       }
       
       - (id)copy {
           return _instance;
       }
       
       - (id)mutableCopy {
           return _instance;
       }
      

二. MRC环境下的单例模式

  1. 与ARC相比, 目前使用MRC的项目基本也就是一些大公司的老项目, 所以建议只作为拓展知识理解一下

  2. MRC单例的步骤

    • 在类的内部提供一个static修饰的全局变量
    • 提供一个类方法方便外界调用: shareXXX
    • 重写+allocWithZone方法, 保证永远只分配一次内存
    • 谨慎起见, 可以重写-copyWithZone-mutableCopyWithZone方法, 每次都返回单例对象
    • 重写-release方法, 单例对象不能被释放掉
    • 重写-retain方法, 单例对象不需要被多次引用, 每次retain只返回单例对象即可
    • 建议重写-retainCount方法, 随意给一个最大值, 提醒外界这是一个单例方法
  3. 注意点:

    • 在这里可以使用一个条件编译, 写一份单例代码, 可以同时供ARC和MRC一起使用
    • #if __has_feature(objc_arc) #else使用这个条件编译
      • 如果当前是ARC的话, 就不需要重写-retain等MRC方法
      • 如果是MRC的话, 再通过条件编译将MRC内存管理方法加入编译
  4. 代码示例:

     //1. 创建静态变量
     static FHTool *_instance;
     
     + (instancetype)shareFHTool {
         return [[FHTool alloc] init];
     }
     
     // 2. 重写实例化方法
     + (instancetype)allocWithZone:(struct _NSZone *)zone {
         
     //    @synchronized(self) {
     //        if (_instance == nil) {
     //            _instance = [super allocWithZone:zone];
     //        }
     //    }
         
         static dispatch_once_t onceToken;
         dispatch_once(&onceToken, ^{
             _instance = [super allocWithZone:zone];
         });
         
         return _instance;
     }
     
     - (id)copy {
         return _instance;
     }
     
     - (id)mutableCopy {
         return _instance;
     }
     
     #if __has_feature(objc_arc)
     
     #else 
     
     // 重写release,让单例对象不能被release
     - (oneway void)release {
         
     }
     
     // 重写retain,单例不需要修改引用计数
     - (instancetype)retain {
         return _instance;
     }
     
     // 查询当前引用计数,返回最大值即可
     - (NSUInteger)retainCount {
         return MAXFLOAT;
     }
     
     #endif
    

三. 将单例模式封装为宏

  1. 可以使用宏, 来讲单例模式的代码封装到一个PCH全局性宏文件中

  2. 但是不推荐这样使用, 按照苹果的建议, 在程序中应该尽量少用PCH文件

  3. 对于一个程序来说, 单例并不会有很多, 因此没有太大的必要给单例代码定义一个全局宏

  4. 因此代码仅作为了解

     #ifdef __OBJC__
     // H文件
     #define SingleH(name) +(instancetype)share##name;
     
     #if __has_feature(objc_arc)
     // M文件,ARC
     #define SingleM(name) static id _instance;\
     + (instancetype)share##name {\
         return [[self alloc] init];\
     }\
     + (instancetype)alloc {\
         static dispatch_once_t onceToken;\
         dispatch_once(&onceToken, ^{\
             _instance = [super alloc];\
         });\
         return _instance;\
     }
     
     #else
     
     // M文件,MRC
     #define SingleM(name) static id _instance;\
     + (instancetype)share##name {\
     return [[self alloc] init];\
     }\
     + (instancetype)alloc {\
         static dispatch_once_t onceToken;\
         dispatch_once(&onceToken, ^{\
             _instance = [super alloc];\
         });\
         return _instance;\
     }\
     - (id)copy {\
         return _instance;\
     }\
     - (id)mutableCopy {\
         return _instance;\
     }
     
     
     #endif
     
     #endif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问 从而方便地控制了实例个数,并...
    喝完酒再来杯拉菲阅读 150评论 0 1
  • 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问 从而方便地控制了实例个数,并...
    JonesCxy阅读 385评论 0 0
  • 什么是单例模式? >是开发设计模式(共23种)中的1种 >它可以保证在程序运行过程,一个类只有一个实例(一个对象)...
    泥孩儿0107阅读 255评论 0 0
  • 线程间的通信 从子线程回到主线程 延时执行 iOS常见的延时执行有两种方式p 调用NSObject的方法 p 使用...
    一抹月光3053阅读 769评论 1 12
  • 让我们用自己的行和自己的心去教育我们的孩子。一切为了孩子,为了一切孩子,为了孩子的一切。用欣赏的眼光看待孩子,让尊...
    文文Jean阅读 257评论 0 0