美文网首页
单例模式

单例模式

作者: serenade07 | 来源:发表于2016-12-15 19:49 被阅读0次

这篇文章的主要内容包括:
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

相关文章

  • 【设计模式】单例模式

    单例模式 常用单例模式: 懒汉单例模式: 静态内部类单例模式: Android Application 中使用单例模式:

  • Android设计模式总结

    单例模式:饿汉单例模式://饿汉单例模式 懒汉单例模式: Double CheckLock(DCL)实现单例 Bu...

  • 2018-04-08php实战设计模式

    一、单例模式 单例模式是最经典的设计模式之一,到底什么是单例?单例模式适用场景是什么?单例模式如何设计?php中单...

  • 设计模式之单例模式详解

    设计模式之单例模式详解 单例模式写法大全,也许有你不知道的写法 导航 引言 什么是单例? 单例模式作用 单例模式的...

  • Telegram开源项目之单例模式

    NotificationCenter的单例模式 NotificationCenter的单例模式分析 这种单例模式是...

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • IOS单例模式的底层原理

    单例介绍 本文源码下载地址 1.什么是单例 说到单例首先要提到单例模式,因为单例模式是单例存在的目的 单例模式是一...

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • 单例模式

    单例模式1 单例模式2

  • java的单例模式

    饿汉单例模式 懒汉单例模式

网友评论

      本文标题:单例模式

      本文链接:https://www.haomeiwen.com/subject/ixaimttx.html