1. 单例模式的作用

  • 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问
  • 从而方便地控制了实例个数,并节约系统资源

2. 单例模式的使用场合

  • 在整个应用程序中,共享一份资源(这份资源只需要创建初始化1次)

3. ARC中,单例模式的实现

在.m中保留一个全局的static的实例

1.ARC

@interface HMDataTool : NSObject

+ (instancetype)sharedDataTool;

@end

@implementation HMDataTool

// 用来保存唯一的单例对象

static id _instace;

//重载了allocWithZone:,保持了从sharedInstance方法返回的单例对象,使用者哪怕使用alloc时也会返回唯一的 实例(alloc方法中会先调用allocWithZone:创建对象)。而retain等内存管理的函数也被重载了,这样做让我们有了把 Singleton类变得“严格”了。

+ (id)allocWithZone:(struct _NSZone *)zone

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_instace = [super allocWithZone:zone];

});

return _instace;

}

+ (instancetype)sharedDataTool

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_instace = [[self alloc] init];

});

return _instace;

}

- (id)copyWithZone:(NSZone *)zone

{

return _instace;

}

@end

简化单例模式

定义一个.h文件,只要包含该头文件,就可以用宏直接定义单例内容

// .h文件
#define HMSingletonH(name) + (instancetype)shared##name; // .m文件
#if __has_feature(objc_arc) #define HMSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
} #else #define HMSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
} \
\
- (oneway void)release { } \
- (id)retain { return self; } \
- (NSUInteger)retainCount { return ;} \
- (id)autorelease { return self;} #endif

调用时:

@interface HMMusicTool : NSObject
HMSingletonH(MusicTool)
@end
@implementation HMMusicTool
HMSingletonM(MusicTool)
@end

2.非ARC

@interface HMDataTool : NSObject

+ (instancetype)sharedDataTool;

@end

@implementation HMDataTool

// 用来保存唯一的单例对象

static id _instace;

+ (id)allocWithZone:(struct _NSZone *)zone

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_instace = [super allocWithZone:zone];

});

return _instace;

}

+ (instancetype)sharedDataTool

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_instace = [[self alloc] init];

});

return _instace;

}

- (id)copyWithZone:(NSZone *)zone

{

return _instace;

}

- (oneway void)release {

}

- (id)retain {

return self;

}

- (NSUInteger)retainCount {

return 1;

}

- (id)autorelease {

return self;

}

@end

 
 

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. java设计模式单例模式 ----懒汉式与饿汉式的区别

    常用的五种单例模式实现方式 ——主要: 1.饿汉式(线程安全,调用率高,但是,不能延迟加载.) 2.懒汉式(线程安全,调用效率不高,可以延时加载.) ——其他: 1.双重检测锁式(由于JVM底层内部模 ...

  6. Python设计模式--单例模式(懒汉式)

    1. 单例模式 --> 单一(唯一)的实例. 在整个运行时间内, 内存中只有一个对象, 一般该对象涉及网络,资源等操作. 2. 单例模式一般分为懒汉式和饿汉式 懒汉式内存占用更加合理. 3. 调 ...

  7. iOS 单例模式 浅叙

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

  8. Java单例模式--------懒汉式和饿汉式

    单件模式用途:单件模式属于工厂模式的特例,只是它不需要输入参数并且始终返回同一对象的引用.单件模式能够保证某一类型对象在系统中的唯一性,即某类在系统中只有一个实例.它的用途十分广泛,打个比方,我们开发 ...

  9. IOS 单例模式的学习

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

随机推荐

  1. svn访问权限控制

    [customer:/]qa = rwreadonly = ryinqixian = r@haowu_partner_dev = r@admin = rw[customer:/branches/v1. ...

  2. 谷歌下设置滚动条的css样式

    .oLi-lists-scroll::-webkit-scrollbar { width:5px;  padding:1px; background:url(../images/repeat-bar. ...

  3. UVA 299 (13.07.30)

     Train Swapping  At an old railway station, you may still encounter one of the lastremaining ``train ...

  4. 空循环比较 for foreach array_map array_walk

    申请一个数组,然后不断的跑空循环,看看执行时间 for循环 foreach (不使用键) foreach(使用键) array_map array_walk 查看效率速度发现很明显 是foreach更 ...

  5. SVN遇到的几个错误问题解决办法

    1.svn更新被锁 清理之后陷入死循环问题 Attempted to lock an already-locked dir svn: Working copy 'E:\Workspaces\eclip ...

  6. java常用string inputStream转换

    1.String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes()); 或者 ByteArrayInp ...

  7. 利用sqlmap和burpsuite绕过csrf token进行SQL注入 (转)

    问题:post方式的注入验证时遇到了csrf token的阻止,原因是csrf是一次性的,失效导致无法测试. 解决方案:Sqlmap配合burpsuite,以下为详细过程,参照国外牛人的blog(不过 ...

  8. 补丁安装命令(WUSA)

    wusa windows6.1-kb2716513-x64.msu /quiet /norestart msxml4-kb2758694-enu.exe /quiet /norestart 安装.ms ...

  9. poj 1147 Binary codes

    Binary codes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5647   Accepted: 2201 Desc ...

  10. iOS开发——数据持久化Swift篇&iCloud云存储

    iCloud云存储 import UIKit class ViewController: UIViewController { override func viewDidLoad() { super. ...