iOS单例

作者: Cheriez | 来源:发表于2017-02-15 10:28 被阅读6次

    一:什么是单例?

    在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死,该实例对象就不会被释放。

    二:单例作用

    1. 在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如在APP开发中我们可能在任何地方都要使用用户的信息,那么可以在登录的时候就把用户信息存放在一个文件里面,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息。这种方式简化了在复杂环境下的配置管理。

    2. 有的情况下,某个类可能只能有一个实例。比如说你写了一个类用来播放音乐,那么不管任何时候只能有一个该类的实例来播放声音。再比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个,这里就可以通过单例模式来避免两个打印任务同时输出到打印机中,即在整个的打印过程中我只有一个打印程序的实例。

    三:单例创建

    1. GCD方式创建单例

    static id _instance;

    + (instancetype)allocWithZone:(struct _NSZone *)zone

    {

           static dispatch_once_t onceToken;

           dispatch_once(&onceToken, ^{

                  _instance = [super allocWithZone:zone];

           });

           return _instance;

    }

    + (instancetype)sharedInstance

    {

           static dispatch_once_t onceToken;

           dispatch_once(&onceToken, ^{

                   _instance = [[self alloc] init];

          });

          return _instance;

    }

    - (id)copyWithZone:(NSZone *)zone

    {

             return   _instance;

    }

    - (id)mutableCopyWithZone:(NSZone *)zone {

              return   _instance;

    }

    2. 互斥锁方式

    static id _instance;

    + (instancetype)allocWithZone:(struct _NSZone *)zone

    {

            @synchronized(self) {

                    if (_instance == nil) {

                    _instance = [super allocWithZone:zone];

            }

    }

              return _instance;

    }

    + (instancetype)sharedInstance

    {

              @synchronized(self) {

               if (_instance == nil) {

                    _instance = [[self alloc] init];

                }

    }

              return _instance;

    }

    - (id)copyWithZone:(NSZone *)zone

    {

              return _instance;

    }

    注意:互斥锁会影响性能,所以最好还是使用GCD方式创建单例。

    四:宏创建单例

    Singleton.h文件

    ==================================

    #define SingletonH(name) + (instancetype)shared##name;

    #define SingletonM(name) \

    static id _instance; \

    \

    + (instancetype)allocWithZone:(struct _NSZone *)zone \

    { \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

    _instance = [super allocWithZone:zone]; \

    }); \

    return _instance; \

    } \

    \

    + (instancetype)shared##name \

    { \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

    _instance = [[self alloc] init]; \

    }); \

    return _instance; \

    } \

    \

    - (id)copyWithZone:(NSZone *)zone \

    { \

    return _instance; \

    }\

    \

    - (id)mutableCopyWithZone:(NSZone *)zone { \

    return _instance; \

    }

    调用方式是:

    viewcontroller.h文件

    ===========================

    #import

    #import "Singleton.h"

    @interface ViewController : UIViewController

    SingletonH(viewController)

    @end

    viewcontroller.m文件

    ===========================

    @interface ViewController ()

    @end

    @implementation ViewController

    SingletonM(ViewController)

    - (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"%@ %@ %@ %@", [ViewController sharedViewController],[ViewController sharedViewController], [[ViewController alloc] init],[[ViewController alloc] init]);

    }

    @end

    相关文章

      网友评论

        本文标题:iOS单例

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