iOS Objective-C实现单例模式的两种方法

第一种,通过@synchronized实现。
AccessTokenTool1.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface AccessTokenTool1 : NSObject

@property (nonatomic, assign) NSInteger tokenType;

// 单例模式1
+ (AccessTokenTool1 *)getShareInstance;

// 获取Token
- (NSString *)getAccessToken;

@end

NS_ASSUME_NONNULL_END

AccessTokenTool1.m

#import "AccessTokenTool1.h"

static AccessTokenTool1 *singletonTool = nil;

@interface AccessTokenTool1()

@end

@implementation AccessTokenTool1

#pragma mark - SingletonPattern

// 单例模式1
+ (AccessTokenTool1 *)getShareInstance {
    @synchronized (self) {
        if (singletonTool == nil) {
            singletonTool = [[self alloc] init];
        }
        
        return singletonTool;
    }
}

// 获取Token
- (NSString *)getAccessToken {
    if (self.tokenType == 0) {
        return @"e10adc3949ba59abbe56e057f20f883e";
    }
    else {
        return @"c33367701511b4f6020ec61ded352059";
    }
}

@end

调用者ViewController.m

#import "ViewController.h"
#import "AccessTokenTool1.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 获取单例1
    AccessTokenTool1 *tool = [AccessTokenTool1 getShareInstance];
    tool.tokenType = 0;
    NSLog(@"token = %@", [tool getAccessToken]);
}

@end
控制台输出日志信息:
2020-02-29 19:46:44.280786+0800 SingletonPatternDemo[26358:511145] token = e10adc3949ba59abbe56e057f20f883e

第二种,通过dispatch_once_t实现。
AccessTokenTool2.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface AccessTokenTool2 : NSObject

@property (nonatomic, assign) NSInteger tokenType;

// 单例模式2
+ (AccessTokenTool2 *)getShareInstance;

// 获取Token
- (NSString *)getAccessToken;

@end

NS_ASSUME_NONNULL_END

AccessTokenTool2.m

#import "AccessTokenTool2.h"

@interface AccessTokenTool2()

@end

@implementation AccessTokenTool2

#pragma mark - SingletonPattern

// 单例模式2
+ (AccessTokenTool2 *)getShareInstance {
    static AccessTokenTool2 *singletonTool = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        singletonTool = [[self alloc] init];
    });
    
    return singletonTool;
}

// 获取Token
- (NSString *)getAccessToken
{
    if (self.tokenType == 0) {
        return @"e10adc3949ba59abbe56e057f20f883e";
    }
    else {
        return @"c33367701511b4f6020ec61ded352059";
    }
}

@end

调用者ViewController.m

#import "ViewController.h"
#import "AccessTokenTool2.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 获取单例2
    AccessTokenTool2 *tool = [AccessTokenTool2 getShareInstance];
    tool.tokenType = 1;
    NSLog(@"token = %@", [tool getAccessToken]);
}

@end
控制台输出日志信息:
2020-02-29 19:49:30.576020+0800 SingletonPatternDemo[26412:512987] token = c33367701511b4f6020ec61ded352059
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值