美文网首页
iOS单例的几种写法

iOS单例的几种写法

作者: 7_c5dc | 来源:发表于2019-05-18 19:51 被阅读0次

单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例类的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。

一、只用于单线程(多线程不安全)

static Share *_instance = nil;

@implementation Share
//线程不安全
+ (instancetype)shared {
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    if (_instance == nil) {
        _instance = [super allocWithZone:zone];
    }
    return _instance;
}

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

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}

@end

二、使用dispatch_once只创建一次 (推荐)

static Share *_instance = nil;

@implementation Share

+ (instancetype)shared {
    return [[self alloc] init];
}

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

//为了严谨重写copy mutableCopy
- (id)copyWithZone:(NSZone *)zone {
    return _instance;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}

@end

三、同步锁


static Share *_instance = nil;

@implementation Share

+ (instancetype)shared {
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    @synchronized (self) {
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    }
    return _instance;
}

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

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}

@end

四、同步锁2(前后再次非空判断)


static Share *_instance = nil;

@implementation Share

+ (instancetype)shared {
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    if (_instance == nil) {
        @synchronized (self) {
            if (_instance == nil) {
                _instance = [super allocWithZone:zone];
            }
        }
    }
    return _instance;
}

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

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}

@end

相关文章

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • iOS 单例的几种写法

    1、不使用GCD的方式 2、使用GCD // 补充再来说说:Objective-C 里的 Alloc 和Alloc...

  • iOS单例的几种写法

    单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例类的特殊类。通过单例模式可以保证系统中一个类...

  • 单例的2种写法

    单例模式是iOS开发中最常用的设计模式,iOS的单例模式有两种官方写法,如下: 1,常用写法 import "Se...

  • iOS 再谈单例的几种写法

    1.为什么再谈? 常规的单例写法有两种+ (Student *)defaultInstance{static St...

  • iOS-单例的几种写法

    一、静态指针方式(单线程模式单例) 二、多线程加锁单例 三、单例的健壮性 由于水平有限,难免出现纰漏,如有问题还请...

  • 单例模式

    单例设计模式是几种设计模式中比较容易理解的,手写单例模式也是面试频繁问到的。下面总结一下单例模式的几种写法: //...

  • 几种单例的写法

    早上随便翻到单例的写法,那我也随手写几个吧 除了以下几种,需要知道枚举也是属于一种实现方式,并且它可以防止序列化/...

  • 如何实现单例,单例的弊端

    单例在项目中是必不可少的,他可以使我们全局共享我们的数据。 首先,单例写法有好几种,通常的写法是基于线程安全的写法...

  • Java设计模式—单例模式

    概念 java中单例模式是一种常见的设计模式,单例模式的写法有好几种,比较常见的有:懒汉式单例、饿汉式单例。单例模...

网友评论

      本文标题:iOS单例的几种写法

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