OC中的单例设计模式及单例的宏抽取

时间:2023-03-08 19:50:33
OC中的单例设计模式及单例的宏抽取
 // 在一个对象需要重复使用,并且很频繁时,可以对对象使用单例设计模式
// 单例的设计其实就是多alloc内部的allocWithZone下手,重写该方法 #pragma Person.h文件 #import <Foundation/Foundation.h>
@interface Person : NSObject <NSCopying,NSMutableCopying>
+ (instancetype)sharePerson; // 给类提供一个创建单例对象的类工厂方法
@end #pragma Person.m文件 //
// Person.m
// 单例设计
//
// Created by 康生 邱 on 15/11/26.
// Copyright (c) 2015年 康生 邱. All rights reserved.
// #import "Person.h" @implementation Person + (instancetype)sharePerson
{
Person *instance = [[self alloc] init]; // 单例设计可以从alloc内的allocWithZone方法下手
return instance;
}
static Person *_instance = nil;
// 该方法决定了每次创建出来的是否是同一块存储空间的地址
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
// 如果_instance 等于nil,那么就调用父类的alocWithZone来创建一个对象、初始化后赋值给它
if (_instance == nil) {
_instance = [[super allocWithZone:zone] init];
} // 如果_instance 不等于nil,即已经指向了对象,那么直接返回地址即可,这样新创建的对象也指向同一个地址的对象
return _instance;
} - (id)copyWithZone:(NSZone *)zone
{
// copy是由对象调用的,在单例中对象已存在,就不需要创建对象了,直接返回对象指针即可
return _instance;
} - (id)mutableCopyWithZone:(NSZone *)zone
{
// mutableCopy和copy一样,直接返回对象指针就可以
return _instance;
} // 如果在MRC中,还需要重写release、retain、retainCount
- (oneway void)release
{
// 为了保证单例对象在程序结束前不被释放,\
这里什么都不写
}
- (instancetype)retain
{
// 直接返回单例对象地址
return _instance;
}
- (NSUInteger)retainCount
{
// 可以返回一个特殊的值,以提醒其他程序员
// return MAXFLOAT;
return UINT64_MAX;
} @end #pragma - 对单例模式的宏抽取
// 新建一个头文件(Singleton.h) // 可以使用interfaceSingleton替换掉Person类单例类工厂方法声明
#define interfaceSingleton(name) +(instancetype)share##name; // 如果当前工程在ARC下
#if __has_feature(objc_arc)
// ARC
#define implementationSingleton(name) \
+ (instancetype)share##name \
{\
name *instance = [[self alloc] init];\
return instance;\
}\
static name *_instance = nil;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone\
{\
if (_instance == nil) {\
_instance = [[super allocWithZone:zone] init];\
}\
return _instance;\
}\
- (id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}
#else
// MRC
#define implementationSingleton(name) \
+ (instancetype)share##name \
{\
name *instance = [[self alloc] init];\
return instance;\
}\
static name *_instance = nil;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone\
{\
if (_instance == nil) {\
_instance = [[super allocWithZone:zone] init];\
}\
return _instance;\
}\
- (id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (oneway void)release\
{\
}\
- (instancetype)retain\
{\
return _instance;\
}\
- (NSUInteger)retainCount\
{\
return MAXFLOAT;\
}
// 结束条件编译
#endif 通过将单例模式相关代码抽取成一个头文件,以后在类中只要导入待头文件,并传入类名即可 #pragma - 单例宏抽取后的Person.h文件 #import <Foundation/Foundation.h>
#import "Singleton.h"
@interface Person : NSObject <NSCopying,NSMutableCopying> interfaceSingleton(Person) // 宏定义,编译时会把宏名替换成+ (instancetype)sharePerson;
// 参数的传递是告诉宏替换成方法声明的时间share后面是什么名称 @end #pragma - 单例宏抽取后的Person.m文件 #import "Person.h" @implementation Person // 通过宏定义,给实现文件中的代码取别名,在实现中直接写好宏名+ 当前的类名就行了
implementationSingleton(Person)
@end