单例模式
单例模式概念
单例模式通俗一点讲就是一个类有且仅有一个实例,在之前的学习中有学习过单例模式的知识,从代码中可以看出在对实例进行初始化的时候,首先要判断实例是否为nil,如果为nil再去初始化,通俗一点就这个意思。标准一点的概念:这个实例被创建以后,就一直到这个程序(APP)结束后系统才会自动释放这块内存,而且一旦你创建了一个单例类,不论你在多少个界面中初始化调用了这个单例方法取得对象,它们所有的对象都是指向的同一块内存存储空间。
单例类举例:
- UIApplication(应用程序实例类)
- NSNotificationCenter(消息中心类)
- NSFileManager(文件管理类)
- NSUserDefaults(应用程序设置)
- NSURLCache(请求缓存类)
- NSHTTPCookieStorage(应用程序cookies池)
为什么要使用单例模式?
有时候我们需要一个全局的对象,而且要保证全局有且只有一份即可,这时候就需要用到单例设计模式,需要注意:在多线程的环境下做好线程保护。
我们再来复习一下经典static关键字作用
- static修饰局部变量表示将该局部变量存储在静态区
- 修饰全局变量表示限制该全局变量只能在当前文件中访问
- 修饰函数用于限制函数只能在当前源文件中使用
如何实现单例模式
@interface FKDog : NSObject
+ (id)instance;
@end
@implementation FKDog
static id instance = nil;
+ (id)instance {
if (!instance) {
instance = [[super alloc]init];
}
return instance;
}
@end
单例模式的优缺点
- 优点
在内存中只有一个对象,节省内存空间;
避免频繁的创建销毁对象,可以提高性能;
避免对共享资源的多重占用,简化访问;
- 缺点
不易被重写或扩展;
不适用于变化频繁的对象;
如果实例化的对象长时间不被利用,系统会认为该对象是垃圾而被回收,这可能会导致对象状态的丢失;
单例模式两种:
- 懒汉模式:第一次用到单例对象的时候再创建。
- 饿汉模式:一进入程序就创建一个单例对象。(不常用)
懒汉模式
#import <Foundation/Foundation.h>
@interface Singleton : NSObject<NSMutableCopying, NSCopying>
//获取单例
+ (instancetype)sharedSingleton;
@end
//---------------------------------------------
#import "Singleton.h"
@implementation Singleton
static id _instance;
//alloc方法内部会调用这个方法
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
if (_instance == nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance == nil) { // 防止创建多次
_instance = [super allocWithZone:zone];
}
}
}
return _instance;
}
+ (instancetype)sharedSingleton {
if (_instance == nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance == nil) { // 防止创建多次
_instance = [[self alloc] init];
}
}
}
return _instance;
}
- (id)copyWithZone:(NSZone *)zone {
return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return _instance;
}
@end
饿汉模式
#import <Foundation/Foundation.h>
@interface Singleton : NSObject<NSMutableCopying, NSCopying>
//获取单例
+ (instancetype)sharedSingleton;
@end
//---------------------------------------------
#import "Singleton.h"
@implementation Singleton
static id _instance;
//当类加载到OC运行时环境中(内存),就会调用一次(一个类只会加载1次)
+ (void)load{
_instance = [[self alloc] init];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (_instance == nil) { // 防止创建多次
_instance = [super allocWithZone:zone];
}
return _instance;
}
+ (instancetype)sharedSingleton{
return _instance;
}
- (id)copyWithZone:(NSZone *)zone{
return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return _instance;
}
@end
static关键字修饰局部变量:
- 当static关键字修饰局部变量时,只会初始化一次且在程序中只有一份内存;
- 关键字static不可以改变局部变量的作用域,但可延长局部变量的生命周期(直到程序结束才销毁)。
static关键字修饰全局变量:
- 当static关键字修饰全局变量时,作用域仅限于当前文件,外部类是不可以访问到该全局变量的。