单例模式在软件开发中经常用到,在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】单例模式的更多相关文章

  1. iOS单例模式(Singleton)写法简析

    单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类只能有一个实例: ...

  2. IOS单例模式(Singleton)

    IOS单例模式(Singleton)   单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模 ...

  3. IOS 单例模式的写法

    iOS的单例模式有两种官方写法,如下: 1)不使用GCD的方式 #import "Manager.h" static Manager *manager; @implementati ...

  4. iOS单例模式

    单例模式用于当一个类只能有一个实例的时候, 通常情况下这个“单例”代表的是某一个物理设备比如打印机,或是某种不可以有多个实例同时存在的虚拟资源或是系统属性比如一个程序的某个引擎或是数据.用单例模式加以 ...

  5. iOS 单例模式 浅叙

    单例模式作用 可以保证在程序运行过程中,一个类只有一个实例,而且该实例易于供外界使用 从而方便地控制了实例个数,并节约系统资源 单例模式使用场合 在整个引用程序中,共享一份资源(这份资源只需要创建初始 ...

  6. ios 单例模式(懒汉式)

    1. 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问 从而方便地控制了实例个数,并节约系统资源 2. 单例模式的使用场合 在整个应用程序中,共享一份资源(这份资源 ...

  7. IOS 单例模式的学习

    单例模式只能修改无法释放,直到程序结束. 我们下面一步一步来做一个单例模式程序 (1)单例一旦创建,是永远存在于内存中的,所以需要创建一个全局量 static MySingletonClass *sh ...

  8. iOS – 单例模式写一次就够了

    一. 单例模式简介 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问 从而方便地控制了实例个数,并节约系统资源 单例模式的使用场合 在整个应用程序中,共享一份资源( ...

  9. iOS 单例模式简单实例

    单例模式主要实现唯一实例,存活于整个程序范围内,一般存储用户信息经常用到单例,比如用户密码,密码在登录界面用一次,在修改密码界面用一次,而使用单例,就能保证密码唯一实例.如果不用单例模式,init 两 ...

  10. iOS 单例模式 学习 "52个方法 第6章 45条 使用 dispath_once 来执行只需运行一次的线程安全代码"

    百度定义:单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. 维基百科:在软件工程中,单例是一种用于实现单例的数学概念,即将 ...

随机推荐

  1. EventBus 3.0使用

    在没用eventBus之前一直用Android广播方式通知消息更新UI 广播写法 首先发送广播通知 Intent intent = new Intent(); intent.setAction(&qu ...

  2. 纯CSS实现图片抖动

    实现方法: 1.将上文提到的以下JS内容删除: $(".imagelogo").mouseover(function() { obj = $(this); i = 5; timer ...

  3. 深入剖析 redis AOF 持久化策略

    本篇主要讲的是 AOF 持久化,了解 AOF 的数据组织方式和运作机制.redis 主要在 aof.c 中实现 AOF 的操作. 数据结构 rio redis AOF 持久化同样借助了 struct ...

  4. Erwin 生成 mysql 带注释(comment )的脚本

    Erwin设计数据库非常方便,有逻辑视图和物理视图,可以很方便的生成数据库文档和SQL 脚本.在使用过程中唯一不爽的地方是脚本不能生成comment.   在百度无数次无法解决下,又FQ谷歌,在一个日 ...

  5. 修改 Semantic UI 的默认字体

    Semantic UI 默认使用的是谷歌提供的字体,并且是直接使用了谷歌的官方链接.由于大家都知道的原因,谷歌网站在国内访问速度很差,甚至根本无法访问,还有就是可能会在离线环境下使用 Semantic ...

  6. nlog(n)解动态规划--最长上升子序列(Longest increasing subsequence)

    最长上升子序列LIS问题属于动态规划的初级问题,用纯动态规划的方法来求解的时间复杂度是O(n^2).但是如果加上二叉搜索的方法,那么时间复杂度可以降到nlog(n).  具体分析参考:http://b ...

  7. Ranorex 5 发布,支持SAP、Oracle Forms、MS Dynamics等

    Ranorex 5 发布,支持SAP.Oracle Forms.MS Dynamics等 http://blog.csdn.net/testing_is_believing/article/detai ...

  8. swift 附属脚本

    附属脚本是访问对象,集合或序列的快捷方式 struct STest{ let constValue:Int subscript(count:Int)->Int{ return count*con ...

  9. Eclipse中如何修改SVN的地址

    在SVN服务端的IP更改后,客户端SVN的连接地址可以在Eclipse中进行修改,方法如下: 首先:在Eclipse中选择Windows-> Show View->others 就会出现[ ...

  10. UITabBarController的一些基础设置

    利用代码添加UITabBarController - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...