【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 来执行只需运行一次的线程安全代码"
百度定义:单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. 维基百科:在软件工程中,单例是一种用于实现单例的数学概念,即将 ...
随机推荐
- SSH乱码解决
解决方案: 使用linux,在用户根目录(/root)下有一个.bash_profile配置文件,该配置只对当前用户有效. 使用ls -a命令可以查看到该文件.使用vi编辑器打开该文件后,在其中加入 ...
- 导出 C/C++ API 给 Lua 使用[转]
导出 C/C++ API 给 Lua 使用 cocos2d-x 和 quick-cocos2d-x 的底层代码都是使用 C++ 语言开发的.为了使用 Lua 脚本语言进行开发,我们利用 tolua ...
- 实现快速迭代的引擎设计 - Capcom RE Engine的架构与实现
[译]实现快速迭代的引擎设计 - Capcom RE Engine的架构与实现 ken hu· 6 天前 原文(日文):ラピッドイテレーションを実現するゲームエンジンの設計 CEDEC2016上的一个 ...
- 天朝专用- 配置pypi镜像
使用python者,需要经常安装模块,可是身在天朝.pypi.python.org 站点稳定性相当差,为了很更好的使用pip安装模块. 请使用镜像. mac/linux 环境 在用户当前目录下 如没有 ...
- 获得 LayoutInflater 实例的三种方式
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- jQuery - jQuery的$.extend和$.fn.extend作用及区别
jQuery为开发插件提拱了两个方法,分别是: 1. jQuery.fn.extend(); 2. jQuery.extend(); 虽然 javascript没有明确的类的概念,但是可以构建类似类的 ...
- 关于在for循环中绑定事件打印变量i是最后一次。
其实函数引用的外部变量都是最后一次的值. <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 4.3.3版本之引擎bug
bug描述: IOS设备上,当使用WWW www = WWW.LoadFromCacheOrDownload(url, verNum); 下载资源时,第一次下载某个资源,www.assetBundle ...
- Android性能优化之内存篇
下面是内存篇章的学习笔记,部分内容与前面的性能优化典范有重合,欢迎大家一起学习交流! 1)Memory, GC, and Performance 众所周知,与C/C++需要通过手动编码来申请以及释放内 ...
- Unity3D 角色(物体) 移动方法 合集
1. 简介 在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position. 2. 通过Transform组件移动物体 Transform 组件用于描述物体在 ...