标签:
1 // 2 // ViewController.m 3 // 01-单例设计模式 4 // 5 // Created by mac on 16/4/22. 6 // Copyright © 2016年 mac. All rights reserved. 7 // 8 #import "LYMusicTool.h" 9 10 #import "ViewController.h" 11 12 @interface ViewController () 13 14 @end 15 16 @implementation ViewController 17 /** 18 * 19 1> .static 修饰不同东西有不同作用域:修饰全局变量(当前文件), 20 2> .extern 可以引用其他文件的全局变量 21 22 3> .单例:三部曲(全局id类型静态static对象+接口方法+重写allocWithZone:)+1(接口) 23 1).懒方式-->>第一次用到单例对象时,再创建 24 2).饿汉式-->>程序类一加载就创建单例对象 重写load方法 25 */ 26 27 - (void)viewDidLoad { 28 [super viewDidLoad]; 29 30 // [self textB]; 31 } 32 33 /** 34 * extern引用的使用 35 */ 36 - (void)textB { 37 38 LYMusicTool *tool = [LYMusicTool sharedMusicTool]; 39 //引用某个全局变量,(并非定义) 40 extern id _music; 41 _music = nil; 42 NSLog(@"%@ === %@ ", _music, tool); 43 } 44 45 /** 46 * 1. 懒汉式->单例 47 */ 48 - (void)textA { 49 50 LYMusicTool *musicTool1 = [[LYMusicTool alloc] init]; 51 LYMusicTool *musicTool2 = [musicTool1 copy]; 52 LYMusicTool *musicTool3 = [musicTool2 copy]; 53 LYMusicTool *tool4 = [LYMusicTool sharedMusicTool]; 54 LYMusicTool *tool5 = [LYMusicTool sharedMusicTool]; 55 56 NSLog(@"=%p==%p==%p=", musicTool3, tool4, tool5); 57 } 58 59 @end 60 61 ////////////////////////////////02-华丽的分割线/////////////////////////////// 62 63 // 64 // LYMusicTool.m 65 // 01-单例设计模式 -->>1).懒汉式 66 // 67 // Created by mac on 16/4/22. 68 // Copyright © 2016年 mac. All rights reserved. 69 70 //alloc的本质是调用:-->>allocWithZone:方法 71 72 #import "LYMusicTool.h" 73 74 @implementation LYMusicTool 75 /** 76 * 1. 创建一个全局变量,作用域比较广泛,所有的文件中都能够访问 77 */ 78 id _music; 79 80 /** 81 * 2. 重写allocWithZone:只分配一次内存 82 */ 83 + (instancetype)allocWithZone:(struct _NSZone *)zone { 84 85 if (_music == nil) { //防止频繁加锁 86 @synchronized(self) { //加互斥锁防止多线程同时进来访问,更加安全,真正的单例 87 if (!_music) { //防止创建多次 88 _music = [super allocWithZone:zone]; 89 } 90 } 91 } 92 return _music; 93 } 94 95 /** 96 * 3. 既保证了单例(只分配一次内存),又保证了只init一次; 97 */ 98 + (instancetype)sharedMusicTool { 99 100 if (_music == nil) { //防止频繁加锁 101 @synchronized(self) { 102 if (!_music) { //防止:创建多次和多次初始化 103 _music = [[self alloc] init]; 104 } 105 } 106 } 107 return _music; 108 } 109 110 /** 111 * 5.copy使用 112 * 113 * 1). 遵守coping协议 114 * 2). 重写协议方法copyWithZone:拦截copy产生新对象 115 * 3). 不用判断_music是否空,是因为copy的前提是有对象,有对象 所以_music有值 116 */ 117 - (id)copyWithZone:(NSZone *)zone { 118 119 return _music; 120 } 121 @end 122 123 ////////////////////////////////03-华丽的分割线/////////////////////////////// 124 125 // 126 // LYLazyTool.m 127 // 01-单例设计模式 :2)饿汉式 128 // 129 // Created by mac on 16/4/22. 130 // Copyright © 2016年 mac. All rights reserved. 131 // 132 133 #import "LYLazyTool.h" 134 135 @implementation LYLazyTool 136 static id _instance; 137 138 + (instancetype)sharedLazyTool { 139 140 return _instance; 141 } 142 143 + (instancetype)allocWithZone:(struct _NSZone *)zone { 144 145 if (_instance == nil) { 146 _instance = [super allocWithZone:zone]; 147 } 148 return _instance; 149 } 150 151 /** 152 * 当类加载到OC运行时环境中(内存中),就会调用一次(一个类只会加载一次) 153 * 类装载到内存的时候调用 154 */ 155 + (void)load { 156 157 NSLog(@"load---"); 158 _instance = [[self alloc] init]; 159 NSLog(@"load--%@", _instance); 160 } 161 /** 162 * 第一次使用类的时候才会调用 163 */ 164 + (void)initialize { 165 166 NSLog(@"initialize---"); 167 } 168 @end
标签:
原文地址:http://www.cnblogs.com/foreveriOS/p/5420876.html