??????????????????????????
???????????????????????????
????????г???????????????η?????????η?????????
????????????????????????????????м????????????????????????????
// .h???
#define YHSingletonH(name) + (instancetype)shared##name;
// .m???
#define YHSingletonM(name)
/**
*  ????static????????????????????????
*  ???static????????粻?????_instance??????
*/
static id _instance;

/**
*  ?????????????????
*/
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken?? ^
{
_instance = [super allocWithZone:zone];
});

return _instance;
}

/**
*  ????????????Σ????????????????????????
*/
-(instancetype)init
{
__block typeof(self) onceSelf = self;
static dispatch_once_t onceToken;
dispatch_once(&onceToken?? ^
{
onceSelf = [super init];
});

return onceSelf;
}

/**
*  ???????????????????????????
*/
+(instancetype)shared##name
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken?? ^
{
_instance = [[self alloc] init];
});
return _instance;
}

/**
*  ????????????????????????????????<NSCopying>Э??
*/
-(id)copyWithZone:(NSZone *)zone
{
return _instance;
}
??????????
??????.h???
#import <Foundation/Foundation.h>
// ???????????
#import "YHSingleton.h"
@interface YHXXX : NSObject
/** ???? */
@property (nonatomic?? strong) NSString *name;
/** .h????????YHSingletonH???? */
YHSingletonH(XXX)
@end
??.m???
#import "YHXXX.h"
@interface YHXXX()
@end
@implementation YHXXX
/** .m????????YHSingletonM?????????? */
YHSingletonM(XXX)
@end
?????????????ü?????????
static id _instance;???????????????????????????′?????????????£?
// ?????????????
YHSingelton
// YHPerson?????YHSingelton
YHPerson : YHSingelton
// YHCar??????YHSingelton
YHCar : YHSingelton
// ????????????YHPerson????
// ???static id _instance;?????????????????
NSLog(@"%@---%@"?? [YHPerson sharedInstance]?? [[YHCar sharedInstance]);
??????????????????????????????????б??????GCD?????????????дallocWithZone:????????????
// ?????????
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if (_instance == nil)
{
@synchronized(self)
{
if(_instance == nil)
{
_instance = [super allocWithZone:zone];
}
}
}
return _instance;
}