标签:
1 // 在一个对象需要重复使用,并且很频繁时,可以对对象使用单例设计模式 2 // 单例的设计其实就是多alloc内部的allocWithZone下手,重写该方法 3 4 #pragma Person.h文件 5 6 #import <Foundation/Foundation.h> 7 @interface Person : NSObject <NSCopying,NSMutableCopying> 8 + (instancetype)sharePerson; // 给类提供一个创建单例对象的类工厂方法 9 @end 10 11 #pragma Person.m文件 12 13 // 14 // Person.m 15 // 单例设计 16 // 17 // Created by 康生 邱 on 15/11/26. 18 // Copyright (c) 2015年 康生 邱. All rights reserved. 19 // 20 21 #import "Person.h" 22 23 24 25 @implementation Person 26 27 + (instancetype)sharePerson 28 { 29 Person *instance = [[self alloc] init]; // 单例设计可以从alloc内的allocWithZone方法下手 30 return instance; 31 } 32 static Person *_instance = nil; 33 // 该方法决定了每次创建出来的是否是同一块存储空间的地址 34 + (instancetype)allocWithZone:(struct _NSZone *)zone 35 { 36 // 如果_instance 等于nil,那么就调用父类的alocWithZone来创建一个对象、初始化后赋值给它 37 if (_instance == nil) { 38 _instance = [[super allocWithZone:zone] init]; 39 } 40 41 // 如果_instance 不等于nil,即已经指向了对象,那么直接返回地址即可,这样新创建的对象也指向同一个地址的对象 42 return _instance; 43 } 44 45 - (id)copyWithZone:(NSZone *)zone 46 { 47 // copy是由对象调用的,在单例中对象已存在,就不需要创建对象了,直接返回对象指针即可 48 return _instance; 49 } 50 51 - (id)mutableCopyWithZone:(NSZone *)zone 52 { 53 // mutableCopy和copy一样,直接返回对象指针就可以 54 return _instance; 55 } 56 57 58 59 // 如果在MRC中,还需要重写release、retain、retainCount 60 - (oneway void)release 61 { 62 // 为了保证单例对象在程序结束前不被释放,\ 63 这里什么都不写 64 } 65 - (instancetype)retain 66 { 67 // 直接返回单例对象地址 68 return _instance; 69 } 70 - (NSUInteger)retainCount 71 { 72 // 可以返回一个特殊的值,以提醒其他程序员 73 // return MAXFLOAT; 74 return UINT64_MAX; 75 } 76 77 @end 78 79 80 #pragma - 对单例模式的宏抽取 81 // 新建一个头文件(Singleton.h) 82 83 // 可以使用interfaceSingleton替换掉Person类单例类工厂方法声明 84 #define interfaceSingleton(name) +(instancetype)share##name; 85 86 // 如果当前工程在ARC下 87 #if __has_feature(objc_arc) 88 // ARC 89 #define implementationSingleton(name) 90 + (instancetype)share##name 91 { 92 name *instance = [[self alloc] init]; 93 return instance; 94 } 95 static name *_instance = nil; 96 + (instancetype)allocWithZone:(struct _NSZone *)zone 97 { 98 if (_instance == nil) { 99 _instance = [[super allocWithZone:zone] init];100 }101 return _instance;102 }103 - (id)copyWithZone:(NSZone *)zone104 {105 return _instance;106 }107 - (id)mutableCopyWithZone:(NSZone *)zone108 {109 return _instance;110 } 111 #else 112 // MRC 113 #define implementationSingleton(name) 114 + (instancetype)share##name 115 {116 name *instance = [[self alloc] init];117 return instance;118 }119 static name *_instance = nil;120 + (instancetype)allocWithZone:(struct _NSZone *)zone121 {122 if (_instance == nil) {123 _instance = [[super allocWithZone:zone] init];124 }125 return _instance;126 }127 - (id)copyWithZone:(NSZone *)zone128 {129 return _instance;130 }131 - (id)mutableCopyWithZone:(NSZone *)zone132 {133 return _instance;134 }135 - (oneway void)release136 {137 }138 - (instancetype)retain139 {140 return _instance;141 }142 - (NSUInteger)retainCount143 {144 return MAXFLOAT;145 } 146 // 结束条件编译 147 #endif 148 149 150 通过将单例模式相关代码抽取成一个头文件,以后在类中只要导入待头文件,并传入类名即可 151 152 #pragma - 单例宏抽取后的Person.h文件 153 154 #import <Foundation/Foundation.h> 155 #import "Singleton.h" 156 @interface Person : NSObject <NSCopying,NSMutableCopying> 157 158 interfaceSingleton(Person) // 宏定义,编译时会把宏名替换成+ (instancetype)sharePerson; 159 // 参数的传递是告诉宏替换成方法声明的时间share后面是什么名称 160 161 @end 162 163 164 #pragma - 单例宏抽取后的Person.m文件 165 166 #import "Person.h" 167 168 @implementation Person 169 170 // 通过宏定义,给实现文件中的代码取别名,在实现中直接写好宏名+ 当前的类名就行了 171 implementationSingleton(Person) 172 @end
标签:
原文地址:http://www.cnblogs.com/qson1507/p/4999201.html