前言
在前期学习OC初期时曾经简单了解过单例模式,今天通过这篇博客总结近期学习的单例模式。
简介
单例模式是一种常见的设计模式,使用单例模式创建的类在当前的进程中只可以有一个实例,可以防止一个实例被反复创建而占用内存空间。
在开发过程中有许多用到的单例,例如UIApplication,一个UIApplication对象就代表着一个应用程序,每个应用程序有且仅有一个UIApplication对象。
优缺点分析
优点
- 实例控制:单例可以保证系统中该类有且仅有一个实例,可以确保所有对象都访问这个唯一的实例。
- 灵活性:类控制了实例化的过程,所以类可以灵活的更改实例化对象。
- 节省开销:单例模式可以节省内存开发和系统的性能开销。
缺点
- 难以扩展:单例模式通常不容易被子类化或扩展
- 违反单一原则:一个类只有一个对象,在一定程度上违反了单一职责原则。
- 单例实例一旦创建,对象指针是保存在静态区的,那么在堆区分配空间只有在应用程序终止后才会被释放。
两种模式
懒汉模式
在程序第一次使用单例对象的时候再进行创建, 即当需要时在创建,不提前准备。
在之前OC刚开始学习的时候,曾经简单了解过单例模式,下面是当时学习时的代码:
在这个方法中,在多线程的环境中,当多个线程同时访问单例时,也有可能返回不同的对象,所以我们要确保多线程环境下,也只有一个线程在执行代码块,其他线程会被阻塞知道代码块执行完成。
这里我给出两种方式来保证线程安全:
dispatch_once(dispatch_once_t *predicate,dispatch_block_t block)
:使用dispatch_once来保证线程安全十分简洁,在使用单例模式时更加推荐这个方法。
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if(_instance == nil) {
static dispatch_once_t onceToken;//用于标记是否已经执行
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];//创建单例实例
});//当onceToken还没有被执行时,才会进入该代码块
}
return _instance;
}
- 使用
@synchronized
加锁来保证线程安全,这里笔者对于他的实现原理并不太清楚,推荐可以看看这篇博客自行了解:oc中synchronized的实现原理。
+ (instancetype)Singleton
{
if(_instance == nil) {
@synchronized (self) {
if(_instance == nil) {
_instance = [[self alloc]init];
}
}
}
return _instance;
}
下面展示我的代码:
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<NSCopying,NSMutableCopying>
+ (instancetype) Singleton;
@end
ViewCOntroller.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
static id _instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if(_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
}
return _instance;
}
+ (instancetype)Singleton
{
if(_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
}
return _instance;
}
- (void)viewDidLoad {
[super viewDidLoad];
_instance = [[NSString alloc] init];
_instance = @"哇哈哈";
NSLog(@"%@", _instance);
}
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
return _instance;
}
- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {
return _instance;
}
@end
注意事项:
- 在我们初始化对象时,仅完成
[[self alloc] init]
的单例判断,会发现返回的还是不同的对象,也不能仅重写allocWithZone:
方法创建单例,都还是会返回不同的对象。由于初始化对象时要调用重写alloc,故而两者缺一不可。 - 全局变量使用static原因如下:当其修饰局部变量时,生命周期与全局变量相同,直到程序结束,只使用一份内存空间;修饰全局变量时,在其他文件中,可以通过 extern id _instance来声明,然后直接在其他文件中调用。用 static 修饰后在其他文件不能通过 extern id _instance 声明后引用
- 重写NSCopying和NSMutableCopying方法是为了确保不会创建新的副本,也得到同一个单例对象的引用。
饿汉模式
饿汉模式就是当类第一次被床架时就创建好了实例对象,也就是提前准备好了东西,在使用时直接使用即可。
load:
方法:通常在首次启动时或类首次被使用时调用,确保在类的实例话和方法调用之前执行。
下面给出我的代码:
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<NSCopying,NSMutableCopying>
+ (instancetype) Singleton;
@end
ViewCOntroller.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
static id _instance;
+ (void)load
{
_instance = [[self alloc] init];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if(_instance == nil) {
_instance = [super allocWithZone:zone];
}
return _instance;
}
+ (instancetype)Singleton
{
return _instance;
}
- (void)viewDidLoad {
[super viewDidLoad];
_instance = [[NSString alloc] init];
_instance = @"哇哈哈";
NSLog(@"%@", _instance);
}
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
return _instance;
}
- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {
return _instance;
}
@end
总结
懒汉模式 | 饿汉模式 | |
---|---|---|
优点 | 懒汉模式在第一次使用时才会被创建,这样可以节约资源,通过加锁的方法可以确保多线程的安全性。 | 实现简单,不需要考虑线程安全的问题 |
缺点 | 实现复杂的懒汉模式会编写大量的代码,容易出现错误 | 在一开始就被创建,可能会浪费资源 |