【iOS】单例模式
单例模式在软件开发中经常用到,在iOS系统framework也很多地方用到单例模式,例如 [NSUserDefaults standardUserDefaults], [NSBundle mainBundle]等,下面演示一下iOS如何实现单例模式
MRC模式
SingletonClass.h
#import <Foundation/Foundation.h> @interface SingletonClass : NSObject + (SingletonClass *)sharedInstance; @end
SingletonClass.m
#import "SingletonClass.h" @implementation SingletonClass static 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 <Foundation/Foundation.h>
#import "SingletonDefine.h" @interface SingletonClass : NSObject DEFINE_SINGLETON_HEADER(SingletonClass) @end
SingletonClass.h
#import "SingletonClass.h" @implementation SingletonClass DEFINE_SINGLETON_IMPLEMENTATION(SingletonClass) @end
SingletonClass.m
ARC模式
SingletonClass.h
#import <Foundation/Foundation.h> @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 <Foundation/Foundation.h>
#import "SingletonDefine.h" @interface SingletonClass : NSObject DEFINE_SINGLETON_HEADER(SingletonClass) @end
SingletonClass.h
#import "SingletonClass.h" @implementation SingletonClass DEFINE_SINGLETON_IMPLEMENTATION(SingletonClass) @end
SingletonClass.m
【iOS】单例模式的更多相关文章
- iOS单例模式(Singleton)写法简析
单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类只能有一个实例: ...
- IOS单例模式(Singleton)
IOS单例模式(Singleton) 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模 ...
- IOS 单例模式的写法
iOS的单例模式有两种官方写法,如下: 1)不使用GCD的方式 #import "Manager.h" static Manager *manager; @implementati ...
- iOS单例模式
单例模式用于当一个类只能有一个实例的时候, 通常情况下这个“单例”代表的是某一个物理设备比如打印机,或是某种不可以有多个实例同时存在的虚拟资源或是系统属性比如一个程序的某个引擎或是数据.用单例模式加以 ...
- iOS 单例模式 浅叙
单例模式作用 可以保证在程序运行过程中,一个类只有一个实例,而且该实例易于供外界使用 从而方便地控制了实例个数,并节约系统资源 单例模式使用场合 在整个引用程序中,共享一份资源(这份资源只需要创建初始 ...
- ios 单例模式(懒汉式)
1. 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问 从而方便地控制了实例个数,并节约系统资源 2. 单例模式的使用场合 在整个应用程序中,共享一份资源(这份资源 ...
- IOS 单例模式的学习
单例模式只能修改无法释放,直到程序结束. 我们下面一步一步来做一个单例模式程序 (1)单例一旦创建,是永远存在于内存中的,所以需要创建一个全局量 static MySingletonClass *sh ...
- iOS – 单例模式写一次就够了
一. 单例模式简介 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问 从而方便地控制了实例个数,并节约系统资源 单例模式的使用场合 在整个应用程序中,共享一份资源( ...
- iOS 单例模式简单实例
单例模式主要实现唯一实例,存活于整个程序范围内,一般存储用户信息经常用到单例,比如用户密码,密码在登录界面用一次,在修改密码界面用一次,而使用单例,就能保证密码唯一实例.如果不用单例模式,init 两 ...
- iOS 单例模式 学习 "52个方法 第6章 45条 使用 dispath_once 来执行只需运行一次的线程安全代码"
百度定义:单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. 维基百科:在软件工程中,单例是一种用于实现单例的数学概念,即将 ...
随机推荐
- 怎样用UltraISO制作U盘系统安装盘
http://jingyan.baidu.com/article/d169e186800f02436711d87b.html 如今用u盘装系统成为主流,如何不被社会淘汰.跟我往下边看吧~~ 工具/原料 ...
- ps中如何用抽出功能扣取头发
一些图片中需要扣取人的头发,非常不好扣,本文介绍抽取扣除 打开一个人物图片,用ctrj+j分别复制几个图层,从下往上分别为:背景副本,图层2(用于修改成别的背景),图层1抽头发白色(用于抽头发,强制前 ...
- 使用shiro的SimpleHash来生成常用的摘要串
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</ ...
- CentOS下Red5安装
Red5介绍 Red5是一个采用Java开发开源的Flash流媒体服务器.它支持:把音频(MP3)和视频(FLV)转换成播放流: 录制客户端播放流(只支持FLV):共享对象:现场直播流发布:远程调用. ...
- [原]OpenGL基础教程(二)多边形绘制
上篇介绍了最基本的三角形绘制,本篇介绍如何使用索引的方式绘制多边行. 为什么要使用索引方式,总体来说提高性能.如何提高:使用顶点数组的好处是避免大量的函数调用.即避免每画一个顶点就调用1次glVert ...
- 通过PowerShell获取TCP响应(类Telnet)
通常情况下,为了检测指定的TCP端口是否存活,我们都是通过telnet指定的端口看是否有响应来确定,然而默认情况下win8以后的系统默认是不安装telnet的.设想一下如果你黑进了一个服务器,上面没装 ...
- volley中网络请求
首先使用Volley类创建 RequestQueue queue = Volley.newRequestQueue(this); Making GET Requests final String u ...
- 配置新系统(Win7 x64)
新装了一个Win7 x64系统.总结了一些系统配置需要注意的地方. 1. C盘空间 发现C盘被用去了50G的空间,在什么软件都没装的情况下,被用去这么多,感到不可思议. 打开控制面板->文件夹选 ...
- EvreryDay Collect
1.在使用WebService时我们经常会考虑以下问题:怎么防止别人访问我的WebService? 在System.Net中提供了一个NetworkCredential,只有获得该凭证的用户才能访问相 ...
- 14款让前端开发者心动的jQuery/CSS3插件及源码
14款让前端开发者心动的jQuery/CSS3插件及源码,一起来看看. 1.jQuery左右滚动banner代码! DEMO演示 / 源码下载 2.jQuery QQ表情插件qqFace ...