测试
一. 对假单例进行调用
单例写法
// Singleton.m // TestSingleton // // Created by taobaichi on 2017/4/13. // Copyright © 2017年 MaChao. All rights reserved. // #import "Singleton.h" static Singleton * _sharedSingleton = nil; @implementation Singleton +(Singleton *)sharedInstance { if (_sharedSingleton == nil) { _sharedSingleton = [[Singleton alloc]init]; } return _sharedSingleton; } @end
- 调用方法一
// // ViewController.m // TestSingleton // // Created by taobaichi on 2017/4/13. // Copyright © 2017年 MaChao. All rights reserved. // #import "ViewController.h" #import "Singleton.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self testSingleton]; } -(void)testSingleton { Singleton * single1 = [Singleton sharedInstance]; Singleton * single2 = [Singleton sharedInstance]; if (single1 == single2) { NSLog(@"single1 == single2"); } NSLog(@"single1地址 %@",single1); NSLog(@"single2地址 %@",single2); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
打印结果
2017-04-13 13:12:27.327 TestSingleton[2515:106202] single1 == single2 2017-04-13 13:12:27.327 TestSingleton[2515:106202] single1地址 <Singleton: 0x6000000147f0> 2017-04-13 13:12:27.327 TestSingleton[2515:106202] single2地址 <Singleton: 0x6000000147f0>
- 调用方法二:
// // ViewController.m // TestSingleton // // Created by taobaichi on 2017/4/13. // Copyright © 2017年 MaChao. All rights reserved. // #import "ViewController.h" #import "Singleton.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self testSingleton]; } -(void)testSingleton { Singleton * single1 = [[Singleton alloc]init]; Singleton * single2 = [[Singleton alloc]init]; if (single1 == single2) { NSLog(@"single1 == single2"); } NSLog(@"single1地址 %@",single1); NSLog(@"single2地址 %@",single2); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
打印结果:
2017-04-13 13:13:47.526 TestSingleton[2529:108124] single1地址 <Singleton: 0x60800001cda0> 2017-04-13 13:13:47.526 TestSingleton[2529:108124] single2地址 <Singleton: 0x60800001cdb0>
可以看到,假单例当用alloc实例化的时候,生成的并不是一个单例,也就是说并不是同一个对象
二. 对真单例无论怎么实例化生成的都是同一个对象
真单例写法
// // Singleton.m // TestSingleton // // Created by taobaichi on 2017/4/13. // Copyright © 2017年 MaChao. All rights reserved. // #import "Singleton.h" static Singleton * _sharedSingleton = nil; @implementation Singleton +(instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedSingleton = [super allocWithZone:zone]; }); return _sharedSingleton; } -(id)init { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedSingleton = [super init]; }); return _sharedSingleton; } +(instancetype)sharedInstance { return [[self alloc]init]; } +(id)copyWithZone:(struct _NSZone *)zone { return _sharedSingleton; } +(id)mutableCopyWithZone:(struct _NSZone *)zone { return _sharedSingleton; } @end
调用
// // ViewController.m // TestSingleton // // Created by taobaichi on 2017/4/13. // Copyright © 2017年 MaChao. All rights reserved. // #import "ViewController.h" #import "Singleton.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self testSingleton]; } -(void)testSingleton { Singleton * single1 = [[Singleton alloc]init]; Singleton * single2 = [[Singleton alloc]init]; if (single1 == single2) { NSLog(@"single1 == single2"); } NSLog(@"single1地址 %@",single1); NSLog(@"single2地址 %@",single2); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
打印结果
2017-04-13 13:20:31.480 TestSingleton[2562:111462] single1 == single2 2017-04-13 13:20:31.481 TestSingleton[2562:111462] single1地址 <Singleton: 0x608000012b00> 2017-04-13 13:20:31.481 TestSingleton[2562:111462] single2地址 <Singleton: 0x608000012b00>