iOS中的单例模式
来源:互联网 发布:云数据库的好处 编辑:程序博客网 时间:2024/05/18 20:49
单例的概念
单例类保证了应用程序的生命周期中有且仅有一个该类的实例对象,而且易于外界访问。在iOS中有很多单例类,比如UIApplication
,UIScreen
,NSNotificationCenter
,NSFileManager
,NSUserDefaults
,NSURLCache
,NSHTTPCookieStorage
等等。
单例模式的使用场合
在整个应用程序中,共享一份资源(这份资源只需要创建初始化1次),一般用于工具类。例如:登陆控制器,网络数据请求,音乐播放器等一个工程需要使用多次的控制器或方法。
单例模式实现方法
一、ARC环境下单例模式的实现代码,以Singleton
类为例
//.h文件#import <Foundation/Foundation.h>@interface Singleton : NSObject//为了使实例易于外界访问 我们一般提供一个类方法,类方法命名规范 share类名|default类名|类名+(instancetype)shareSingleton;@end//.m文件#import "Singleton.h"@implementation Singleton//0.提供全局变量static id _instance;//1.alloc会调用allocWithZone:+(instancetype)allocWithZone:(struct _NSZone *)zone{ //实现单例模式的方法1 /* //加互斥锁解决多线程访问安全问题 @synchronized(self) { if (_instance == nil) { _instance = [super allocWithZone:zone]; } } */ //实现单例模式的方法2:使用GCD提供的一次性代码 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance;}//2. 实现类方法(单例方法)+(instancetype)shareSingleton{ return [[self alloc]init];}//3.copy在底层 会调用copyWithZone://为了严谨,也要重写copyWithZone 和 mutableCopyWithZone-(id)copyWithZone:(NSZone *)zone{ return _instance;}-(id)mutableCopyWithZone:(NSZone *)zone{ return _instance;}
二、MRC环境下单例模式的实现代码
在ARC代码基础上重写下面的三个方法即可
/*ARC代码段,详见上文*///什么都不做 保证单例对象不被销毁-(oneway void)release{}//返回本身 保证只有一个单例对象-(instancetype)retain{ return _instance;}//将引用计数设为最大值(习惯)-(NSUInteger)retainCount{ return MAXFLOAT;}
三、在ARC和MRC环境下都适用的单例代码
利用条件编译来判断是ARC还是MRC环境
在ARC代码基础上,利用条件编译判断是否重写下列方法。
/*ARC代码段,详见上文*/#if __has_feature(objc_arc) //如果是ARC环境,不需要重写#else //如果是MRC环境,则重写下列代码-(oneway void)release{}-(instancetype)retain{ return _instance;}-(NSUInteger)retainCount{ return MAXFLOAT;}#endif
四、多个类可以共同使用的单例代码(ARC环境和MRC环境均适用)
创建文件Single.h(文件名称不固定),将上面的代码抽出一个宏,如下:
/*1 宏定义后面如果要替换字符,需要用##拼接2 宏定义后边如果出现换行,需要用符号“ \ ” 来标记下一行也是宏定义的部分,但最后一行末尾不需要*///.h文件 ##表示拼接字符#define SingleH(name) +(instancetype)share##name;#if __has_feature(objc_arc) //条件满足 ARC#define SingleM(name) static id _instance;\+(instancetype)allocWithZone:(struct _NSZone *)zone{\static dispatch_once_t onceToken;\dispatch_once(&onceToken, ^{\_instance = [super allocWithZone:zone];\});\return _instance;\}\+(instancetype)share##name{\return [[self alloc]init];\}\-(id)copyWithZone:(NSZone *)zone{\return _instance;\}\-(id)mutableCopyWithZone:(NSZone *)zone{\return _instance;\}#else //MRC#define SingleM(name) static id _instance;\+(instancetype)allocWithZone:(struct _NSZone *)zone{\static dispatch_once_t onceToken;\dispatch_once(&onceToken, ^{\_instance = [super allocWithZone:zone];\});\return _instance;\}\+(instancetype)share##name{\return [[self alloc]init];\}\-(id)copyWithZone:(NSZone *)zone{\return _instance;\}\-(id)mutableCopyWithZone:(NSZone *)zone{\return _instance;\}\-(oneway void)release{}\-(instancetype)retain{\ return _instance;\}\-(NSUInteger)retainCount{\ return MAXFLOAT;\}#endif
假设我们要在类Person中使用,调用方法如下:
1、在项目中导入文件Singel.h。
2、Person类中的代码
Person.h文件===========================#import <Foundation/Foundation.h>#import "Single.h"@interface PersonSingleH(Person)@endPerson.m文件===========================#import "Person.h"@implementation Person.hSingleM(Person)@end
注意:
单例模式不可以使用继承,因为使用继承,同时也会继承静态变量,当子类和父类同时创建的时候只会创建一个先创建的实例对象。
阅读全文
0 0
- iOS中的单例模式
- IOS中的单例模式
- IOS 中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- IOS中的单例模式
- IOS中的单例模式
- iOS中的单例模式
- iOS 中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- ios中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- Dubbo——容错机制
- How To Create a Custom Salesforce Communities Login and Landing Page
- WebView中打不开文件管理器
- JSF事件机制(一)
- lua添加面向对象
- iOS中的单例模式
- RSA学习一
- phpcms制作的网站后台从本地上传到服务器空间的修改配置和注意事项
- 2017/5/15--计算机系统基础课堂笔记
- Vue之模态框确认组件---使用$emit触发父组件的methods
- 解决浏览器跨域的几种方式
- 在安卓4.x上使用Switch控件,并且替换thumb和track的图片时,Switch展示的width会明显大于设置的宽度,导致图片变形
- hdu1232通畅工程
- DOcker 启动chrome问题解决