美文网首页
iOS 单利模式

iOS 单利模式

作者: 一直很安静_25ae | 来源:发表于2019-07-14 10:58 被阅读0次

单利模式

单例模式的作用

  • 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问从而方便地控制了实例个数,并节约系统资源

单例模式的使用场合
-在整个应用程序中,共享一份资源(这份资源只需要创建初始化1次)


ARC中,单例模式的实现

在.m中保留一个全局的static的实例
static id _instance;

重写allocWithZone:方法,在这里创建唯一的实例(注意线程安全)

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

提供1个类方法让外界访问唯一的实例

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

实现copyWithZone:方法

  • (id)copyWithZone:(struct _NSZone *)zone
    {
    return _instance;
    }

#import <Foundation/Foundation.h>

@interface XMGPerson : NSObject
/** 名字 */
@property (nonatomic, strong) NSString *name;

+ (instancetype)sharedPerson;
@end


#import "XMGPerson.h"

@interface XMGPerson() <NSCopying>

@end

@implementation XMGPerson

static XMGPerson *_person;
//因为alloc内部会调用allocWithZone
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _person = [super allocWithZone:zone];
    });
    return _person;
}

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

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

单利模式提示宏

// .h文件
#define XMGSingletonH(name) + (instancetype)shared##name;

// .m文件
#define XMGSingletonM(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; \
}

单利模式宏的使用

#import <Foundation/Foundation.h>
#import "XMGSingleton.h"

@interface XMGPerson : NSObject
/** 名字 */
@property (nonatomic, strong) NSString *name;

XMGSingletonH(Person)
@end
#import "XMGPerson.h"

@interface XMGPerson()

@end

@implementation XMGPerson
XMGSingletonM(Person)
@end
#import "ViewController.h"
#import "XMGPerson.h"
@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
 [XMGPerson sharedPerson];
}

@end

非GCD实现单利模式

#import <Foundation/Foundation.h>

@interface XMGPerson : NSObject
+ (instancetype)sharedInstance;
@end
#import "XMGPerson.h"

@interface XMGPerson()

@end

@implementation XMGPerson

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;
}
@end

相关文章

  • iOS 单利模式

    单利模式 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问从而方便地控制了实例...

  • GCD练习

    GCD练习 ios 多线程 GCD : ios 多线程 全剧队列,异步执行 线程间通信 信号量 文件锁 单利模式 ...

  • JavaJavascript基础进阶(十七)JS中常用的设计模式

    单利设计模式、构造原型设计模式、发布订阅设计模式、promise设计模式 单利模式 构造原型设计模式 最贴近OOP...

  • 单例模式

    单利模式

  • 单利设计模式

    单利模式的介绍 单利模式是应用最广的模式之一,也可能是很多初级工程师唯一会使用的设计模式。在应用这个模式时,单利对...

  • 细品 javascript 设计模式(单利模式)

    我尽量用最少的文字,最少的篇幅,讲明白设计模式的方方面面。文章连接 理解单利模式 上代码:通用的惰性单利模式 单利...

  • 设计模式

    单利模式 代理模式 工厂模式 装饰者模式:

  • 2018-05-14

    单利设计模式 懒汉式 单例模式 饿汉式 单利模式 懒汉式与饿汉式的区别: 双重锁式 单例模式 (DCL )

  • 单利模式

    11

  • 单利模式

    简介 定义:保证一个类仅有一个实例,并提供唯一一个全局访问他的节点。 1,饿汉模式 public Class Si...

网友评论

      本文标题:iOS 单利模式

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