美文网首页
iOS 如何创建单例对象

iOS 如何创建单例对象

作者: 海强_hq | 来源:发表于2015-09-19 01:29 被阅读2577次

前言 单例模式在iOS开发中是一种常见的设计模式,它在整个程序运行的过程中只有一分实例,可以节约系统资源,今天主要学习如何创建一个单例对象

开发中如果经常会用到一个类,那就没必要每次都释放它,然后又重新再去创建这个类。占用系统资源

开始学习

单例模式的作用

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

单例模式的使用场合

  • 在整个应用程序中,共享一份资源(这份资源只需要创建初始化1次)

创建单例

创建一个类:singleTool

在.h文件中

// 一般情况下, 如果一个类是单例, 那么都会提供一个类方法用于快速创建单例对象
// 而且这个类方法的名称是有一定的规则: share + 类名称 / default + 类名称 / 类名称开头
#import <Foundation/Foundation.h>
@interface singleTool : NSObject <NSCopying,NSMutableCopying>
+ (instancetype)shareSingleTools;
@end

在.m文件中

#import "singleTool.h"
@implementation singleTool

提供一个快速创建的类方法

ARC

#pragma mark ARC
+ (instancetype)shareSingleTools
{
    return [[self alloc] init];
}

注意这里用的是self,不要直接用类名写死,没有拓展性

重写allocWithZone方法

注意:创建对象:都是alloc、new、类方法创建,但内部本质还是[alloc init],所以要考虑alloc内部所调用的方法

static singleTool *_instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
 
// 由于alloc方法内部会调用allocWithZone: 所以我们只需要保证在该方法只创建一个对象即可
    dispatch_once(&onceToken,^{

// 只执行1次的代码(这里面默认是线程安全的)
        _instance = [super allocWithZone:zone];

    });
    
    return _instance;
}

1.static定义的是一个静态的全局变量,会存放在静态区里面,只会保留一份,赋过一次值之后,就不再为nil 2.分配存储空间的代码只执行一次,这样保证了无论是单线程还是多线程都只创建一次对象

重写copy和mutableCopy内部的方法

因为copy方法必须通过实例对象调用, 所以可以直接返回_instance

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

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

到这里ARC状态下的单例对象创建已经可以了,接下来是在MRC状态下的单例对象如何创建

MRC

重写内部方法

重写release方法

#pragma mark MRC
- (oneway void)release
{
}

系统就不会释放对象,单例永远保存在内存中

重写retain方法

- (instancetype)retain
{
    return _instance;
}

当别人retain的时候。把对象返回给它,相当于没有retain

重写retainCount方法

- (NSUInteger)retainCount
{
    return MAXFLOAT;
}

和系统单例一样会返回一个很大的值,提高代码的阅读性

重写dealloc方法

- (void)dealloc
{
    [super dealloc];
}
@end

到这里ARC和MRC模式可以创建单例对象

存在的问题

问题?:如果在ARC状态下,那么MRC重写的方法会报错,也就是说这个类不能同时在ARC和MRC模式满足创建单例的条件,所以这个时候可以尝试将代码抽取成宏,提高代码的复用性,无论在ARC还是MRC , 或在那个程序中都能正常使用

判断当前是ARC还是MRC的状态的方法
#if __has_feature(objc_arc)
NSLog(@“是ARC”);
#else  
NSLog(@“是MRC”);

以后要用直接拖入这个文件,包含头文件即可
在.h中 调用 interfaceSingle(类名);
在.m中调用 implementationSingle(类名)

单例抽取成宏

#define interfaceSingle(name)  + (instancetype)share##name

#if __has_feature(objc_arc)  // 如果满足这个特征说明是在ARC状态下 
// 如果是ARC
#define implementationSingle(name)  + (instancetype)share##name \
{ \
return [[self alloc] init]; \
} \
static id _instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \
- (id)mutableCopyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
#else  
// 如果不是ARC
#define implementationSingle(name)  + (instancetype)share##name \
{ \
return [[self alloc] init]; \
} \
static id _instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
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

相关文章

  • iOS 如何创建单例对象

    前言 单例模式在iOS开发中是一种常见的设计模式,它在整个程序运行的过程中只有一分实例,可以节约系统资源,今天主要...

  • iOS 如何创建单例对象

    前言 单例模式在iOS开发中是一种常见的设计模式,它在整个程序运行的过程中只有一分实例,可以节约系统资源,今天主...

  • iOS 单例模式 - 单例对象销毁【GCD】

    单例对象的创建方式 单例.h 文件的实现 单例的.m 文件的实现 单例对象的销毁【GCD创建的方式】 使用单例对象...

  • iOS 单例模式 - 单例对象销毁【@synchronized】

    单例对象的创建方式 单例.h 文件的实现 单例.m 文件的实现 单例对象的销毁【@synchronized创建方式...

  • ios 开发中的单例模式

    其实iOS开发中的单例模式无非就是一个类创建的对象在程序中只有一个对象! iOS中的单例模式有分为赖汉式和饿汉式单...

  • 单例模式、异常、模块

    单例模式 创建单例-保证只有1个对象 创建单例时,只执行1次init方法 目的 —— 让 类 创建的对象,在系统中...

  • 正确编写单例2018-07-27

    在iOS开发中,有很多地方都选择使用单例模式。有很多时候必须要创建一个对象,并且不能创建多个,用单例就为了防止创建...

  • 【浅析iOS中常用设计模式】

    单例模式 单例模式是iOS开发中常用的一种设计模式,目的在于创建一次对象,多地方使用。系统中的单例例如:UIApp...

  • ❓iOS中有哪些锁? @synchronized 一般在创建单例对象的时候使用,来保证在多线程环境下,创建对象是唯...

  • iOS 单例对象的创建

    static PSDrawerManager *psManager = nil;+(PSDrawerManager...

网友评论

      本文标题:iOS 如何创建单例对象

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