博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【iOS】单例模式
阅读量:5217 次
发布时间:2019-06-14

本文共 8842 字,大约阅读时间需要 29 分钟。

单例模式在软件开发中经常用到,在iOS系统framework也很多地方用到单例模式,例如 [NSUserDefaults standardUserDefaults], [NSBundle mainBundle]等,下面演示一下iOS如何实现单例模式

MRC模式

SingletonClass.h

#import 
@interface SingletonClass : NSObject+ (SingletonClass *)sharedInstance;@end

 

SingletonClass.m

#import "SingletonClass.h"@implementation SingletonClassstatic SingletonClass *_singletonInstance = nil;                     + (instancetype)sharedInstance{                                     @synchronized(self){                                                if (!_singletonInstance) {                                          _singletonInstance = [[self alloc] init];                   }                                                           }                                                               return _singletonInstance;                                  }                                                               + (id)allocWithZone:(NSZone *)zone{                                 @synchronized(self){                                                if (!_singletonInstance) {                                          _singletonInstance = [super allocWithZone:zone];            }                                                               return _singletonInstance;                                  }                                                               return nil;                                                 }                                                               - (instancetype)copyWithZone:(NSZone *)zone;                    {                                                                   return self;                                                }                                                               - (instancetype)retain                                          {                                                                   return self;                                                }                                                               - (unsigned)retainCount                                         {                                                                   return UINT_MAX;                                            }                                                               - (instancetype)autorelease                                     {                                                                   return self;                                                }                                                               - (oneway void)release                                          {                                                               }                                                               @end

懒人技巧:把单例的定义与实现定义成宏

//单例头宏#define DEFINE_SINGLETON_HEADER(className)  \    + (className *)sharedInstance;          \//单例实现宏#define DEFINE_SINGLETON_IMPLEMENTATION(className)              \static className *_singletonInstance = nil;                     \+ (instancetype)sharedInstance{                                 \    @synchronized(self){                                        \        if (!_singletonInstance) {                              \            _singletonInstance = [[self alloc] init];           \        }                                                       \    }                                                           \    return _singletonInstance;                                  \}                                                               \                                                                \+ (id)allocWithZone:(NSZone *)zone{                             \    @synchronized(self){                                        \        if (!_singletonInstance) {                              \            _singletonInstance = [super allocWithZone:zone];    \        }                                                       \        return _singletonInstance;                              \    }                                                           \    return nil;                                                 \}                                                               \                                                                \- (instancetype)copyWithZone:(NSZone *)zone;                    \{                                                               \    return self;                                                \}                                                               \                                                                \- (instancetype)retain                                          \{                                                               \    return self;                                                \}                                                               \                                                                \- (unsigned)retainCount                                         \{                                                               \    return UINT_MAX;                                            \}                                                               \                                                                \- (instancetype)autorelease                                     \{                                                               \    return self;                                                \}                                                               \                                                                \- (oneway void)release                                          \{                                                               \}                                                               \
SingletonDefine
#import 
#import "SingletonDefine.h"@interface SingletonClass : NSObjectDEFINE_SINGLETON_HEADER(SingletonClass)@end
SingletonClass.h
#import "SingletonClass.h"@implementation SingletonClassDEFINE_SINGLETON_IMPLEMENTATION(SingletonClass)                                                        @end
SingletonClass.m

 

ARC模式

SingletonClass.h

#import 
@interface SingletonClass : NSObject+ (instancetype)sharedInstance;//禁用alloc,init,new 创建对象,否则编译会报错+(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));-(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));+(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));@end

SingletonClass.m

#import "SingletonClass.h"@implementation SingletonClass+(instancetype) sharedInstance {                                static dispatch_once_t predicate;                           static SingletonClass *instance = nil;    dispatch_once(&predicate, ^{                                    instance = [[super alloc] initUniqueInstance];          });                                                         return instance;                                        }                                                           -(instancetype) initUniqueInstance {                            return [super init];                                    }                                                           - (instancetype)copyWithZone:(NSZone *)zone                 {                                                               return self;                                            }@end

懒人模式 

//单例头宏(ARC)#define DEFINE_SINGLETON_HEADER(className)                  \+ (instancetype)sharedInstance;                             \//单例实现宏(ARC)#define DEFINE_SINGLETON_IMPLEMENTATION(className)                  \+(instancetype) sharedInstance {                                    \    static dispatch_once_t predicate;                               \    static className *_singletonInstance = nil;                     \    dispatch_once(&predicate, ^{                                    \        _singletonInstance = [[super alloc] init];                  \    });                                                             \    return _singletonInstance;                                      \}                                                                   \                                                                    \- (instancetype)copyWithZone:(NSZone *)zone                         \{                                                                   \    return self;                                                    \}                                                                   \
SingletonDefine.h
#import 
#import "SingletonDefine.h"@interface SingletonClass : NSObjectDEFINE_SINGLETON_HEADER(SingletonClass)@end
SingletonClass.h
#import "SingletonClass.h"@implementation SingletonClassDEFINE_SINGLETON_IMPLEMENTATION(SingletonClass)@end
SingletonClass.m

 

转载于:https://www.cnblogs.com/bomo/p/4504520.html

你可能感兴趣的文章
css样式写一个三角形
查看>>
比callback更简洁的链式执行promise
查看>>
android permission
查看>>
javascript获取textarea中所选文本的开始位置、结束位置和选择的文本
查看>>
【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
查看>>
事务备份还原分离附加
查看>>
JSch - Java实现的SFTP(文件上传详解篇)
查看>>
一些注意点
查看>>
.net 文本框只允许输入XX,(正则表达式)
查看>>
C#修饰符
查看>>
20.核心初始化之异常向量表
查看>>
[BSGS][哈希]luogu P3846 可爱的质数
查看>>
Python 第四十五章 MySQL 内容回顾
查看>>
iostat参数说明
查看>>
js 封装获取元素的第一个元素
查看>>
iOS 获取Home键指纹验证
查看>>
Python-Mac 安装 PyQt4
查看>>
P2571 [SCOI2010]传送带
查看>>
哈希表1
查看>>
用Data Url (data:image/jpg;base64,)将小图片生成数据流形式
查看>>