单例模式

来源:互联网 发布:网络盒子全网通 编辑:程序博客网 时间:2024/05/20 00:13

单例模式的作用

可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问

从而方便地控制了实例个数,并节约系统资源 

单例模式的使用场合

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

单例模式在ARC\MRC环境下的写法有所不同,需要编写2套不同的代码

可以用宏判断是否为ARC环境

#if __has_feature(objc_arc)

// ARC

#else

// MRC

#endif

单例模式- ARC

ARC中,单例模式的实现

.m中保留一个全局的static的实例

static id _instance;

 

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

+ (id)allocWithZone:(struct_NSZone *)zone {

    if (_instance== nil) {  防止频繁加锁

        @synchronized(self) {

            if(_instance== nil) {  防止创建多次

  _instance = [superallocWithZone:zone];

            }

        }

    }

    return_instance;

} 

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

+ (instancetype)sharedMusicTool {

    if (_instance== nil) { 防止频繁加锁

        @synchronized(self) {

            if(_instance== nil) { 防止创建多次

               _instance = [[self alloc] init];

            }

        }

    }

    return_instance;

}

 

实现copyWithZone:方法

- (id)copyWithZone:(struct_NSZone *)zone {

    return_instance;

}

单例模式 ARC

ARC中(MRC),单例模式的实现(比ARC多了几个步骤)

实现内存管理方法

- (id)retain {return self; }

- (NSUInteger)retainCount {return 1; }

- (oneway void)release {}

- (id)autorelease {return self; }

 

1.单例模式

- 1.1 概念相关

1)单例模式

在程序运行过程,一个类只有一个实例

 

2)使用场合

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

 

- 1.2 ARC实现单例

1)步骤

01 在类的内部提供一个static修饰的全局变量

02 提供一个类方法,方便外界访问

03 重写+allocWithZone方法,保证永远都只为单例对象分配一次内存空间

04 严谨起见,重写-copyWithZone方法和-MutableCopyWithZone方法

 

2)相关代码

提供一个static修饰的全局变量,强引用着已经实例化的单例对象实例

static XMGTools *_instance;

 

类方法,返回一个单例对象

+(instancetype)shareTools

{

    注意:这里建议使用self,而不是直接使用类名Tools(考虑继承)

    

    return [[self alloc]init];

}

 

保证永远只分配一次存储空间

+(instancetype)allocWithZone:(struct _NSZone *)zone

{

    使用GCD中的一次性代码

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

    _instance = [super allocWithZone:zone];

        });

    

    使用加锁的方式,保证只分配一次存储空间

    @synchronized(self) {

        if (_instance == nil) {

            _instance = [super allocWithZone:zone];

        }

    }

    return _instance;

}

 

 1. mutableCopy创建一个新的可变对象,并初始化为原对象的值,新对象的引用计数为 1

 2. copy返回一个不可变对象。分两种情况:(1)若原对象是不可变对象,那么返回原对象,并将其引用计数加1;(2)若原对象是可变对象,那么创建一个新的不可变对象,并初始化为原对象的值,新对象的引用计数为1

让代码更加的严谨

-(nonnull id)copyWithZone:(nullable NSZone *)zone

{

    return [[self class] allocWithZone:zone];

    return _instance;

}

 

-(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone

{

    return _instance;

}

- 1.3 MRC实现单例

1)实现步骤

01 在类的内部提供一个static修饰的全局变量

02 提供一个类方法,方便外界访问

03 重写+allocWithZone方法,保证永远都只为单例对象分配一次内存空间

04 严谨起见,重写-copyWithZone方法和-MutableCopyWithZone方法

05 重写release方法

06 重写retain方法

07 建议在retainCount方法中返回一个最大值

2)配置MRC环境知识

01 注意ARC不是垃圾回收机制,是编译器特性

02 配置MRC环境:build setting ->搜索automatic ref->修改为NO

 

3)相关代码

 

提供一个static修饰的全局变量,强引用着已经实例化的单例对象实例

static XMGTools *_instance;

 

类方法,返回一个单例对象

+(instancetype)shareTools

{

    注意:这里建议使用self,而不是直接使用类名Tools(考虑继承)

    return [[self alloc]init];

}

 

保证永远只分配一次存储空间

+(instancetype)allocWithZone:(struct _NSZone *)zone

{

    使用GCD中的一次性代码

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

        _instance = [super allocWithZone:zone];

        });

    

    使用加锁的方式,保证只分配一次存储空间

    @synchronized(self) {

        if (_instance == nil) {

            _instance = [super allocWithZone:zone];

        }

    }

    return _instance;

}

 

让代码更加的严谨

-(nonnull id)copyWithZone:(nullable NSZone *)zone

{

    return [[self class] allocWithZone:zone];

    return _instance;

}

 

-(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone

{

    return _instance;

}

 

MRC环境下,如果用户retain了一次,那么直接返回instance变量,不对引用计数器+1

如果用户release了一次,那么什么都不做,因为单例模式在整个程序运行过程中都拥有且只有一份,程序退出之后被释放,所以不需要对引用计数器操作

-(oneway void)release

{

}

 

-(instancetype)retain

{

    return _instance;

}

 

惯用法,有经验的程序员通过打印retainCount这个值可以猜到这是一个单例

-(NSUInteger)retainCount

{

    return MAXFLOAT;

}

 

 

- 1.4 通用版本

 

1)有意思的对话

 

01 :写一份单例代码在ARCMRC环境下都适用?

答:可以使用条件编译来判断当前项目环境是ARC还是MRC

 

02 :条件编译的代码呢,么么哒?

答:条件编译

#if __has_feature(objc_arc)

如果是ARC,那么就执行这里的代码1

#else

如果不是ARC,那么就执行代理的代码2

#endif

 

03 问:在项目里面往往需要实现很多的单例,比如下载、网络请求、音乐播放等等,弱弱的问一句单例可以用继承吗?

答:单例是不可以用继承的,如果想写一次四处使用,那么推荐亲使用带参数的宏定义啦!

 

04 问:宏定义怎么弄?

答:这个嘛~~回头看一眼我的代码咯,亲。

 

2)使用带参数的宏完成通用版单例模式代码

01 注意条件编译的代码不能包含在宏定义里面

02 宏定义的代码只需要写一次就好,之后直接拖到项目中用就OK

0 0