单例模式通俗一点讲就是一个类有且仅有一个实例,在之前的学习中有学习过单例模式的知识,从代码中可以看出在对实例进行初始化的时候,首先要判断实例是否为nil,如果为nil再去初始化,通俗一点就这个意思。标准一点的概念:这个实例被创建以后,就一直到这个程序(APP)结束后系统才会自动释放这块内存,而且一旦你创建了一个单例类,不论你在多少个界面中初始化调用了这个单例方法取得对象,它们所有的对象都是指向的同一块内存存储空间。
有时候我们需要一个全局的对象,而且要保证全局有且只有一份即可,这时候就需要用到单例设计模式,需要注意:在多线程的环境下做好线程保护。
@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