美文网首页
objective-c 单例设计模式以及单例宏抽取

objective-c 单例设计模式以及单例宏抽取

作者: 一世长安乱 | 来源:发表于2018-04-10 16:46 被阅读0次
  • 什么是单例
    一个类只允许有一个实例,在整个程序中需要多次使用,共享同一份资源的时候,就可以创建单例,一般封装成工具类使用,苹果封装成单例常用的

  • 什么情况下使用单例

  1. 类只能有一个实例,并且必须从一个为人熟知的访问点对其进行访问,比如类工厂方法
  2. 这个唯一的实例只能通过子类化进行扩展,而且扩展的对象不会破坏客户端的代码
  • 设计要点
  1. 某个类只有一个实例
  2. 必须自行创建这个对象
  3. 必须自行向整个系统提供这个实例
  4. 这个方法一定是一个静态类

ARC中

+ (instancetype)shareInstance
{
    Tools *instance = [[self alloc] init];
    return instance;
}
//创建一个全局变量,控制对象只调用一次
static Tools *_instance = nil;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    //所有的创建方法都会调用该方法,只需要在这里控制当前对象之创建一次即可
    if (_instance == nil) {
        _instance = [[super allocWithZone:zone] init];
    }
    return _instance;
}
//用到copy要给类加协议,因为是单例,所以直接返回就行了
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

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

但是该方法在多线程中会出毛病,只需要在重写allocWithZone中这样修改

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    //再多线程中也只执行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:zone] init];
    });
    return _instance;
}

MRC

只需要多重写release,retain,retainCount

- (oneway void)release
{
    //需要让程序在整个过程中只有一个实例,什么都不做
}

- (instancetype)retain
{
    return _instance;
}

- (NSUInteger)retainCount
{
    //方便沟通,不会返回1,返回一个巨大的值
    return MAXFLOAT;
}

单例宏抽取

反斜杠代表一行

#define interfaceSingleton(name) +(instancetype)share##name
//判断是ARC还是MRC
#if __has_feature(objc_arc)

#define implementationSingleton(name) \
+ (instancetype)share##name \
{ \
name *instance = [[self alloc] init]; \
return instance; \
} \
static  name *_instance = nil; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[super allocWithZone:zone] init]; \
}); \
return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \
- (id)mutableCopyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \

#else

#define implementationSingleton(name) \
+ (instancetype)share##name \
{ \
name *instance = [[self alloc] init]; \
return instance; \
} \
static  name *_instance = nil; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[super allocWithZone:zone] init]; \
}); \
return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \
- (id)mutableCopyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \
- (oneway void)release \
{ \
} \
- (instancetype)retain \
{ \
return _instance; \
} \
- (NSUInteger)retainCount \
{ \
return MAXFLOAT; \
}
#endif

相关文章

  • objective-c 单例设计模式以及单例宏抽取

    什么是单例一个类只允许有一个实例,在整个程序中需要多次使用,共享同一份资源的时候,就可以创建单例,一般封装成工具类...

  • iOS 单例

    Objective-C 单例宏 Swift 单例声明

  • iOS单例一行实现(抽取单例宏)

    本文首先实现单例模式,然后对单例代码进行抽取宏,使其他类可以一句代码实现单例(只介绍ARC环境)本文代码 - 单例...

  • 单例模式和GCD单例实现

    1、传统单例模式2、GCD单例模式3、用宏实现GCD单例模式4、用宏实现GCD单例模式,名称随类名变化而变化 单例...

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • 2018-04-08php实战设计模式

    一、单例模式 单例模式是最经典的设计模式之一,到底什么是单例?单例模式适用场景是什么?单例模式如何设计?php中单...

  • python中OOP的单例

    目录 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • 单例

    目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • python 单例

    仅用学习参考 目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计...

  • iOS-单例模式

    swift的单例设计模式 OC的单例设计模式 新学习一种单例思想

网友评论

      本文标题:objective-c 单例设计模式以及单例宏抽取

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