美文网首页
单例设计模式

单例设计模式

作者: 皆为序幕_ | 来源:发表于2018-09-21 13:31 被阅读0次

概念

一个类只允许有一个实例,在整个程序中需要多次使用,共享同一份资源的时候,就可以创建单例,一般封装成工具类使用,苹果封装成单例常用的有UIApplication,NSUserDefaults,NSNotificationCenter,NSFileManager等等

优缺点

  • 优点:因为单例模式会使类只有一个实例所以方便使用,并且节省内存资源的分配.因为使用GCD的方式是线程安全的,所以会避免资源的多重使用
  • 缺点: 单例创建的内存只有在程序结束时才会被释放. 由于单例不能被继承(因为返回的是同一个实例),所以扩展性很不好

实现

一般情况下创建一个单例对象都有一个与之对应的类方法
一般情况下用于创建单例对象的方法名都以share或default开头加上当前类名

  • GCD简单实现
static Person * _instance = nil;
+(instancetype) sharePerson{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance= [[self alloc] init];
    });
    return _instance;
}


Person *p = [Person sharePerson];
NSLog(@"p:%p",p);
Person *p1 = [[Person alloc]init];
NSLog(@"p1:%p",p1);

这种不能算是真正意义上的单例 ,因为类方法和alloc init创建的单例分配的内存可能不一样
p :0x600000c1da40
p1:0x600000c76c60

因为alloc会执行allocWithZone,所以如果想只分配一次内存就要重写此方法,同时为了严谨,防止copy出现以上问题,还要重写copyWithZonemutableCopyWithZone

+ (instancetype)sharePerson{
    Person *instance = [[self alloc]init];
    return instance;
}
static Person *_instance = nil;
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    //由于所有的创建方法都会调用这个方法,所以只需要在该方法中控制当前对象只创建一次即可
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:zone]init];
    });
    return _instance;
}
-(id)copyWithZone:(NSZone *)zone{
    return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
    return _instance;
}

Person *p = [Person sharePerson];
NSLog(@"p:%p",p);
Person *p1 = [[Person alloc]init];
NSLog(@"p1:%p",p1);

p :0x600001fb3700
p1:0x600001fb3700

宏定义单例

#define interfaceSinglenton(name) +(instancetype)share##name

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

宏定义单例的使用

@interface Person : NSObject
@property (nonatomic,strong) NSString *firstName;
@property (nonatomic,copy) NSString *lastName;
interfaceSinglenton(Person);
@end

#import "Person.h"
@implementation Person
implementationSinglenton(Person);
@end


Person *p = [Person sharePerson];
NSLog(@"p :%p",p);
Person *p1 = [[Person alloc]init];
NSLog(@"p1:%p",p1);

log:
p :0x6000027bcb00
p1:0x6000027bcb00

相关文章

  • 单例模式Java篇

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

  • python中OOP的单例

    目录 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • 单例

    目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • python 单例

    仅用学习参考 目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计...

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

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

  • 设计模式第二篇、单例设计模式

    目录1、什么是单例设计模式2、单例设计模式的简单实现3、单例设计模式面临的两个问题及其完整实现4、单例设计模式的应...

  • 设计模式 - 单例模式

    设计模式 - 单例模式 什么是单例模式 单例模式属于创建型模式,是设计模式中比较简单的模式。在单例模式中,单一的类...

  • 2、创建型设计模式-单例设计模式

    江湖传言里的设计模式-单例设计模式 简介:什么是单例设计模式和应用 备注:面试重点考查 单例设计模式:这个是最简单...

  • 设计模式之单例模式

    单例设计模式全解析 在学习设计模式时,单例设计模式应该是学习的第一个设计模式,单例设计模式也是“公认”最简单的设计...

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

网友评论

      本文标题:单例设计模式

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