---
title: "iOS单例模式"
author: "折句号"
date: "2017-05-02T09:14:51Z"
tags: []
categories: ["移动开发"]
slug: "ios"
draft: false
lastmod: "2017-05-02T09:14:51Z"
id: 2372207
url: "https://404530969.iteye.com/blog/2372207"
---

1. 单例模式的作用  
   可以保证在程序运行过程，一个类只有一个实例，而且该实例易于供外界访问  
   从而方便地控制了实例个数，并节约系统资源
2. 单例模式的使用场合  
   在整个应用程序中，共享一份资源（这份资源只需要创建初始化1次），一般用于工具类。例如：登陆控制器，网络数据请求，音乐播放器等一个工程需要使用多次的控制器或方法。
3. 单例模式的优缺点  
   优点：  
   单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问，从而方便对实例个数的控制并节约系统资源。  
   如果希望在系统中某个类的对象只能存在一个，单例模式是最好的解决方案。  
   单例模式因为类控制了实例化过程，所以类可以更加灵活修改实例化过程。  
   缺点：  
   单例对象一旦建立，对象指针是保存在静态区的，单例对象在堆中分配的内存空间，会在应用程序终止后才会被释放。  
   单例类无法继承，因此很难进行类的扩展。  
   单例不适用于变化的对象，如果同一类型的对象总是要在不同的用例场景发生变化，单例就会引起数据的错误，不能保存彼此的状态。

注意：我们在使用单例类之前，一定要考虑好单例类是否适合和类以后的扩展性，避免盲目滥用单例

 

ARC中单例实现步骤

 

> 1 在类的内部提供一个static修饰的全局变量  
> 2 提供一个类方法，方便外界访问  
> 3 重写+allocWithZone方法，保证永远都只为单例对象分配一次内存空间  
> 4 严谨起见，重写-copyWithZone方法和-MutableCopyWithZone方法ARC中单例代码实现
> 
> ```
> #import "Tools.h"

@implementation Tools
// 创建静态对象 防止外部访问
static Tools *_instance;
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
//    @synchronized (self) {
//        // 为了防止多线程同时访问对象，造成多次分配内存空间，所以要加上线程锁
//        if (_instance == nil) {
//            _instance = [super allocWithZone:zone];
//        }
//        return _instance;
//    }
    // 也可以使用一次性代码
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    });
    return _instance;
}
// 为了使实例易于外界访问 我们一般提供一个类方法
// 类方法命名规范 share类名|default类名|类名
+(instancetype)shareTools
{
    //return _instance;
    // 最好用self 用Tools他的子类调用时会出现错误
    return [[self alloc]init];
}
// 为了严谨，也要重写copyWithZone 和 mutableCopyWithZone
-(id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
    return _instance;
}
> ```
> 
> ## 三. 单例在MRC中的实现
> 
> MRC单例实现步骤
> 
> > 1 在类的内部提供一个static修饰的全局变量  
> > 2 提供一个类方法，方便外界访问  
> > 3 重写+allocWithZone方法，保证永远都只为单例对象分配一次内存空间  
> > 4 严谨起见，重写-copyWithZone方法和-MutableCopyWithZone方法  
> > 5 重写release方法  
> > 6 重写retain方法  
> > 7 建议在retainCount方法中返回一个最大值
> 
> 配置MRC环境
> 
> > 1 注意ARC不是垃圾回收机制，是编译器特性  
> > 2 配置MRC环境：build setting -&gt;搜索automatic ref-&gt;修改为N0
> 
> MRC中单例代码实现  
> 配置好MRC环境之后，在ARC代码基础上重写下面的三个方法即可
> 
> ```
> -(oneway void)release
{

}
-(instancetype)retain
{
    return _instance;
}
-(NSUInteger)retainCount
{
    return MAXFLOAT;
}
> ```
> 
> ## 四. 一劳永逸，单例模式的优化
> 
> 如果想要一劳永逸，我们将面临两个问题  
> 1：如何写一份单例代码在ARC和MRC环境下都适用？  
> 2：如何使一份单例代码可以多个类共同使用  
> 为了解决这两个问题，我们可以在PCH文件使用代参数的宏和条件编译  
> 利用条件编译来判断是ARC还是MRC环境
> 
> ```
> #if __has_feature(objc_arc)
//如果是ARC，那么就执行这里的代码1
#else
//如果不是ARC，那么就执行代理的代码2
#endif
> ```
> 
> 注意：单例模式不可以使用继承，因为使用继承，同时也会继承静态变量，当子类和父类同时创建的时候只会创建一个先创建的实例对象。  
> 废话不多说了直接上代码  
> PCH文件Single.h
> 
> ```
> #define singleH(name) +(instancetype)share##name;

#if __has_feature(objc_arc)

#define singleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
    static dispatch_once_t onceToken;\
    dispatch_once(&onceToken, ^{\
        _instance = [super allocWithZone:zone];\
    });\
    return _instance;\
}\
\
+(instancetype)share##name\
{\
    return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
    return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
    return _instance;\
}
#else
#define singleM static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)shareTools\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
}\
\
-(instancetype)retain\
{\
    return _instance;\
}\
\
-(NSUInteger)retainCount\
{\
    return MAXFLOAT;\
}
#endif
> ```
> 
> 这时我们就可以一劳永逸，任何项目中，当我们要使用单例类的时候只要在项目中导入PCH文件然后  
> 在.h文件中调用`singleH(类名)`  
> 在.m文件中调用`singleM(类名)`  
> 创建类时直接调用`share类名`方法即可。
> 
> ```
> @implementation WMSVoicePickingController
static WMSVoicePickingController *sharedVoicePickingController = nil;

+(WMSVoicePickingController *)shareVoicePicking{
    if (sharedVoicePickingController == nil) {
        sharedVoicePickingController = [[WMSVoicePickingController alloc] init];
    }
    return sharedVoicePickingController;
}

+(void)cleanVoicePicking{
    if (sharedVoicePickingController) {
        sharedVoicePickingController = nil;
    }
}

> ```