单例模式

2016-12-15  本文已影响0人  serenade07

这篇文章的主要内容包括:
1、在ARC中完成一个单例模式的三步
2、在MRC中完成一个单例模式的三步
3、单例模式通用宏(最重要)

单例模式是一种常用的软件设计模式。
在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例,节约系统的资源。

在整个程序中,共享一份资源,这个资源只被初始化一次。

0.提供静态全局变量
1.重写allocWithZone
2.提供一个类方法
3.重写copyWithZone和MutableCopyWithZone

#import "XMGTool.h"

@implementation XMGTool

//0.提供全局变量
static XMGTool *_instance;

//1.重写alloc方法
+(instancetype)allocWithZone:(struct _NSZone *)zone{

//    懒加载 永远只分配一次存储空间
//    如果多个线程同时alloc,访问同一块资源,可能涉及线程安全的问题,所以我们可以加上一把互斥锁
//    @synchronized (self) {
//        
//    }if(_instance==nil){
//        _instance=[super allocWithZone:zone];
//    }
//    方法二:GCD一次性代码
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance=[super allocWithZone:zone];
    });

        return _instance;
}


//2.提供一个类方法
//类方法的好处在于:外界方便访问;表明身份
//注意命名规范share+类名/default+类名
+(instancetype)shareTool{
    
    return [[self alloc]init];
    //这里alloc就调用了上面重写的alloc方法 保证只创建一个对象
    
}


//3.重写copy和mutable方法
-(id)copyWithZone:(NSZone *)zone{
    //对象方法,运行之前已经有对象存在了,所以直接返回就可以
    return _instance;
}

-(id)mutableCopyWithZone:(NSZone *)zone{
    return _instance;
}

@end

这样,在控制器中不论用以下任何一种方式创建对象,地址都是相同的,因为只创建一次:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //1.重写allocWithZone
    //单例模式就是不管过程中alloc init多少次 new多少次 都始终是那一个对象
    //单例模式的实现方法:重写alloc方法
    XMGTool *t1=[[XMGTool alloc]init];
    XMGTool *t2=[[XMGTool alloc]init];
    XMGTool *t3=[XMGTool new];
    
    //2.提供一个类方法
    XMGTool *t4=[XMGTool shareTool];
    
    //3.重写copyWithZone和MutableCopyWithZone
    //注意:这两个方法都是对象方法
    XMGTool *t5=[t1 copy];
    XMGTool *t6=[t1 mutableCopy];

    
    NSLog(@"t1:%p----t2:%p----t3:%p----t4:%p----t5:%p----t6:%p",t1,t2,t3,t4,t5,t6);
}

非ARC模式事实上离我们已经很遥远了
事实上,就是重写release和retain以及retainCount方法
这里可以使用一个小技巧 ——条件编译(注意,条件编译不可以用在宏中)
进行判断 如果不满足ARC就执行else和endif之间的

//上接ARC的三个方法
#if __has_feature(objc_arc)
#else
//MRC
-(oneway void)release{
    
}

-(instancetype)retain{
    return _instance;
}

//一个约定俗成的习惯
-(NSUInteger)retainCount{
    return MAXFLOAT;
}
#endif

其实以上两个单例模式都不好
下面介绍在 ARC、MRC都可以实现单例模式的通用宏
用它实现代码的复用,也就是说把单例模式抽取出来,以后用到的时候,在类的h和m文件中包含这个宏就可以了

以下是宏中的代码(条件判断编译不可以放在宏中):

 #define SingleH(name) +(instancetype)share##name;

#if __has_feature(objc_arc)
//ARC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone{\
    static dispatch_once_t onceToken;\
    dispatch_once(&onceToken, ^{\
        _instance=[super allocWithZone:zone];\
    });\
    return _instance;\
}\
+(instancetype)share##name{\
    return [[self alloc]init];\
    }\
-(id)copyWithZone:(NSZone *)zone{\
       return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone{\
    return _instance;\
}



#else
//MRC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance=[super allocWithZone:zone];\
});\
return _instance;\
}\
+(instancetype)share##name{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone{\
return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone{\
return _instance;\
}\
-(oneway void)release{\
}\
-(instancetype)retain{\
    return _instance;\
}\
-(NSUInteger)retainCount{\
    return MAXFLOAT;\
}

#endif


引用示例:


54D6E386-AE5D-4721-A6E1-B975F1D04F7D.png C0BF99D7-831E-4905-A49E-5857374ED02F.png E66A6E95-A367-4F03-A5DD-0D6E02180ECC.png
上一篇 下一篇

猜你喜欢

热点阅读