免费开源的iOS开发学习平台

单例singleton模式–单例类的实现

单例模式的作用是用来解决一个应用中某个类只有唯一一个对象的问题。单例类在iOS开发中是非常重要的,在系统提供个类中,诸如:UIApplication类、NSUserDefaults类、NSNotificationCenter类、NSBundle类都是单例类。

1、单例类的使用

单例类在iOS的应用开发中非常常见,有一些对象必须保证在整个应用中只有一个,当在不同的类中需要使用单例类对象时,能够保证获取的都是同一个对象。

例如,我们常见的QQ音乐里面,在每个节目下方都有一个当前播放音乐的指示器,即便切换不同的界面,这个音乐播放指示器都是统一的,这个就是一个典型的单例类对象。

2、单例类的实现方式

单例类的实现原理就是要保证单例类对象的alloc+init操作,在应用的整个生命周期过程中,只执行一次。当单例类对象被创建后,假如在其他多个地方需要使用该对象,则无需执行alloc+init操作,而是直接返回已经存在的对象即可。

在单例类的实现过程中,为了保证alloc+init只执行一次,可以使用dispatch_once函数,dispatch_once函数的作用就是在整个应用生命周期中执行一次代码块。

单例类的创建过程是完全一致的,因此,单例类的实现代码只要直接复制/粘贴即可。通过下方的代码,就能够实现如下功能:

  • 创建一个单例类;

  • 提供了一个类方法sharedInstance,通过该方法就能够获取到该单例类的对象;

  • 提供了copy方法,调用copy方法时,也是返回已经存在的单例类对象;

(注意:由于ARC已经成为主流,所以以下的单例类创建方法只考虑ARC)

  • .h文件
#import <Foundation/Foundation.h> 
@interface Singleton : NSObject <NSCopying> 
/**
 *  获取单例类
 *
 *  @return 单例类对象
 */
+(instancetype) sharedInstance; 
@end
  • .m文件
#import "Singleton.h" 
@implementation Singleton 

static id _instance;

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
} 

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance; 
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
@end

3、示例代码

通过实际的代码来验证以下单例类的实现效果。

创建三个自定义类:MYPerson、MYStudent、MYDog,其中:MYStudent是MYPerson的子类;

把MYPerson和MYDog定义为单例类,即拷贝上面代码中@interface~@end以及@implementation~@end中的部分,分别到类的.h和.m文件中;

在main()函数中,实现如下代码:

#import <Foundation/Foundation.h>
#import "MYDog.h"
#import "MYPerson.h"
#import "MYStudent.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MYDog *dog1 = [MYDog sharedInstance];
        MYDog *dog2 = [[MYDog alloc] init];
        MYDog *dog3 = [dog1 copy];
        NSLog(@"dog1: %p, dog2: %p, dog3: %p",dog1,dog2,dog3);        
        MYPerson *person1 = [[MYPerson alloc] init];
        MYPerson *person2 = [MYPerson sharedInstance];
        MYStudent *student1 = [[MYStudent alloc] init];
        MYStudent *student2 = [MYStudent sharedInstance];
        NSLog(@"person1: %p, person2: %p, student1: %p, student2: %p",person1,person2,student1,student2);        
    }
    return 0;
}

运行结果如下。我们可以看到,对于MYDog类,不论使用sharedInstance方法、alloc+init,还是copy方法,最后得到的都是同一个对象。对于MYPerson类来说,由于MYPerson类是单例类,因此,其子类也是单例类。

https://github.com/99ios/15.2