iOS单例类详解

原文地址 http://www.cocoachina.com/ios/20160907/17497.html

在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死,该实例对象就不会被释放。下面我们来看看单例的概念、用途、如何创建,以便加深理解。

作用

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

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

创建单例

有两种方法来创建单例,下面分别介绍

1、GCD方式创建单例

staticid _instance;

+ (instancetype)allocWithZone:(struct_NSZone *)zone

{

staticdispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_instance = [super allocWithZone:zone];

});

return_instance;

}

+ (instancetype)sharedInstance

{

staticdispatch_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、互斥锁方式

staticid _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;

}

宏创建单例

如果我们需要在程序中创建多个单例,那么需要在每个类中都写上一次上述代码,非常繁琐。

我们可以使用宏来封装单例的创建,这样任何类需要创建单例,只需要一行代码就搞定了。

实现代码

Singleton.h文件

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

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

#define SingletonM(name) \

staticid _instance; \

\

+ (instancetype)allocWithZone:(struct_NSZone *)zone \

{ \

staticdispatch_once_t onceToken; \

dispatch_once(&onceToken, ^{ \

_instance = [super allocWithZone:zone]; \

}); \

return_instance; \

} \

\

+ (instancetype)shared##name \

{ \

staticdispatch_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中使用,调用方法如下

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

输出结果

可以看到四个对象的内存地址完全一样,说明是同一个对象

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 单例一般作为:工具类 单例命名:一般情况下如果一个类是单例,那么就要提供一个类方法用于快速创建单例对象,而且这个类...
    甘哲157阅读 1,801评论 0 15
  • 在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死...
    零度_不结冰阅读 448评论 0 0
  • 一、单例是什么?(aplɪˈkeɪʃ(ə)n 申请) 在 Foundation 和 Application Kit...
    甘哲157阅读 6,129评论 6 22
  • 在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死...
    VincentHK阅读 687评论 0 3
  • 这个世界上,总有人不停地嚷着无聊,总有好多人喊着迷茫。焦虑、烦躁、拧巴成了多数青年人的通病。 每个人都不想一生碌碌...
    杨同学的同学阅读 1,954评论 47 71