单例模式

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

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

  • 单例模式的应用场景

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

  • 在ARC中完成一个单例模式一共有三步:

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);
}

  • 在MRC中实现单例模式也一共三步

非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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问 从而方便地控制了实例个数,并...
    JonesCxy阅读 385评论 0 0
  • 一. ARC环境下的单例模式 单例模式的基本概念单例, 顾名思义, 即在整个程序中, 某一个类只有唯一一个实例, ...
    面糊阅读 764评论 0 50
  • 近年来各种理论层出不穷,文章里不带点“蓝平长二”之类都不好意思让人看。 为免(了)浪费诸君的宝贵时间,特此对名词做...
    SJTU_CTR阅读 161评论 0 1
  • 周天奶奶给我买了一本优秀作文选,我觉得这篇作文很好,在这里分享给大家。 作文的题目是《我会给动画配...
    徐雨恒阅读 222评论 0 0
  • 今晚被同事呛到哭,难受得无法自抑。 今天的状态很差,绷紧了神经办理业务。旁边一位同事在看考试题准备周末统考,两位在...
    素素_sky阅读 382评论 0 0