单例模式在软件开发中经常用到,在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. OpenSSL命令---pkcs8

    用途: pkcs8格式的私钥转换工具.它处理在PKCS#8格式中的私钥文件.它可以用多样的PKCS#5 (v1.5 and v2.0)和 PKCS#12算法来处理没有解密的PKCS#8 Private ...

  2. linux 文件夹说明,用户添加删除,不熟悉的命令

    一.Linux 根目录下的文件夹说明 usr 程序默认安装路径,相当于windows的 program 附显示当前所处位置:pwd 二.用户 用户添加:useradd 用户名 passwd 用户名 u ...

  3. EF 5.0 帮助类

    EF 5.0 帮助类 加入命名空间: using System; using System.Data; using System.Data.Entity; using System.Data.Enti ...

  4. Solve: Your project references the latest version of Entity Framework (for MySQL) in Visual Studio 2013

    The error message while trying to create a ADO.net Entity Data Model ( Entity Framework 6 ) for MySq ...

  5. Mac Jenkins 权限问题

    在官网下载dmg安装包,安装完毕即可在本机搭建jenkins的工作.但是jenkins不会用本地的用户去构建,任何创建的文件都是“jenkins”用户所有,这会造成很多权限问题,无法调用自己写的脚本, ...

  6. C# RSA PEM解密字符

    1.第一步先用openssl将pem的key转换为der的key //bin>openssl.exe rsa -in rsakeydec.pem -outform der -out pri.de ...

  7. solr多核配置

    假设已经配置好了一个单core的solr服务器. solr.xml配置文件 单核和多核主要在solr.xml配置不同.在solr/example中已经有一个名称为multicore的文件夹里面给我们配 ...

  8. keil l251 command summary --Lib

    keil l251 command summaryLIB251 LIST MYLIB.LIB TO MYLIB.LST PUBLICS LIB251 EXTRACT MYLIB.LIB (GOODCO ...

  9. vxworks下网络编程一:网络字节序问题

    inet_addr("192.168.1.1");//返回网络字节序整型ip地址inet_ntoa(saddr);//将包含网络字节序整型ip地址的in_addr对象转换成本地ch ...

  10. IOS Application Security Testing Cheat Sheet

    IOS Application Security Testing Cheat Sheet    [hide]  1 DRAFT CHEAT SHEET - WORK IN PROGRESS 2 Int ...