1、单例模式简述

  • 一个类有且仅有一个实例,并且自行实例化向整个系统提供。

2、单例模式UML类图

  • 单例模式

3、单例模式示意代码

  • SingletonA

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    /*********  SingletonA.h   **********/
    @interface YSingletonA : NSObject
    + (instancetype)sharedInstance;
    @end

    /********* SingletonA.m **********/
    @implementation YSingletonA

    static YSingletonA *_instance = nil;
    + (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instance = [[[self class] alloc] init];
    });
    return _instance;
    }
    @end

    /********* 使用 **********/
    YSingletonA *singleton1 = [YSingletonA sharedInstance];
    NSLog(@"%@", singleton1);
    YSingletonA *singleton2 = [YSingletonA sharedInstance];
    NSLog(@"%@", singleton2);

    YSingletonA *singleton3 = [[YSingletonA alloc] init];
    NSLog(@"%@", singleton3);
    YSingletonA *singleton4 = [[YSingletonA alloc] init];
    NSLog(@"%@", singleton4);

    YSingletonA *singleton5 = [YSingletonA new];
    NSLog(@"%@", singleton5);
    YSingletonA *singleton6 = [YSingletonA new];
    NSLog(@"%@", singleton6);

    /*
    运行结果:
    <YSingletonA: 0x60000000b550>
    <YSingletonA: 0x60000000b550>

    <YSingletonA: 0x60c00000bc30>
    <YSingletonA: 0x60400000b440>

    <YSingletonA: 0x60400000b450>
    <YSingletonA: 0x60c00000a9f0>

    YSingletonA调用sharedInstance方法可以做到多次创建的实例是同一个
    但是<无法做到>调用原有的init方法或者new方法创建的实例也是同一个
    YSingletonA违背了单例类有且仅有一个实例的定义,或者说做的不够完善
    */
  • SingletonB

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    /*********  SingletonB.h   **********/
    @interface YSingletonB : NSObject <NSCopying, NSMutableCopying>
    + (instancetype)sharedInstance;
    @end

    /********* SingletonB.m **********/
    @implementation YSingletonB

    static YSingletonB *_instance = nil;
    + (instancetype)sharedInstance {
    return [[self alloc] init];
    }

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

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

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

    @end

    /********* 使用 **********/
    YSingletonB *singleton1 = [YSingletonB sharedInstance];
    NSLog(@"%@", singleton1);
    YSingletonB *singleton2 = [YSingletonB sharedInstance];
    NSLog(@"%@", singleton2);

    YSingletonB *singleton3 = [[YSingletonB alloc] init];
    NSLog(@"%@", singleton3);
    YSingletonB *singleton4 = [[YSingletonB alloc] init];
    NSLog(@"%@", singleton4);

    YSingletonB *singleton5 = [YSingletonB new];
    NSLog(@"%@", singleton5);
    YSingletonB *singleton6 = [YSingletonB new];
    NSLog(@"%@", singleton6);


    YSingletonB *singleton7 = [singleton5 copy];
    NSLog(@"%@", singleton7);
    YSingletonB *singleton8 = [singleton5 mutableCopy];
    NSLog(@"%@", singleton8);

    /*
    <YSingletonB: 0x6080000069a0>
    <YSingletonB: 0x6080000069a0>

    <YSingletonB: 0x6080000069a0>
    <YSingletonB: 0x6080000069a0>

    <YSingletonB: 0x6080000069a0>
    <YSingletonB: 0x6080000069a0>

    <YSingletonB: 0x6080000069a0>
    <YSingletonB: 0x6080000069a0>

    YSingletonB调用sharedInstance方法,alloc,init方法,new方法,甚至包括copy、mutableCopy方法我们得到的都是用一个实例
    */
  • 完整Demo