单例模式在软件开发中经常用到,在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. IOS中的网络编程

    在移动互联网时代,几乎所有应用都需要用到网络下载,比如图片的加载,音乐的下载,安装包的下载,等等,下面我们来看看如何进行下载 一.文件的下载我们用get来请求数据,并对请求的二进制数据进行解析存入文件 ...

  2. Webstorm 11 注册/破解方法

    激活时选择第二个,也就是License server在下面输入框中填http://idea.lanyus.com就行了

  3. S1700

  4. WebDriver测试EXT控件(基于C#)

    WebDriver测试EXT控件(基于C#)http://www.docin.com/p-748096409.html

  5. [LeetCode] Additive Number

    Af first I read the title as "Addictive Number". Anyway, this problem can be solved elegan ...

  6. SSAS:菜鸟摸门

    官方:SSAS 多维模型 Analysis Services 多维解决方案使用多维数据集结构来分析多个维度之间的业务数据. 多维模式是 Analysis Services 的默认服务器模式. 它包括针 ...

  7. php 类文件加载 Autoloader

    做习惯了编译语言,转到php 使用 php的面向对象开发时候遇见一个挺别扭的问题.在Php中引入对象 后 在调用过程中还需要将对象所在的php文件 require 到当前php文件 目前代码结构 in ...

  8. Ubuntu桌面版本和服务器版本之间的区别(转载)

    转载自:http://blog.csdn.net/fangaoxin/article/details/6335992 http://www.linuxidc.com/Linux/2010-11/297 ...

  9. 修改mysql默认字符集的方法

    +--------------------------+---------------------------------+ | Variable_name | Value | +---------- ...

  10. Android事件传递机制

    http://blog.csdn.net/awangyunke/article/details/22047987 1)public boolean dispatchTouchEvent(MotionE ...