单例

OC中的单例模式,单例模式在设计模式中用的可能是最多的一种了,而且也是最简单的一种
实现单例模式有三个条件
1、类的构造方法是私有的
2、类提供一个类方法用于产生对象
3、类中有一个私有的自己对象


针对于这三个条件,OC中都是可以做到的
1、类的构造方法是私有的
我们只需要重写allocWithZone方法,让初始化操作只执行一次
2、类提供一个类方法产生对象
这个可以直接定义一个类方法
3、类中有一个私有的自己对象
我们可以在.m文件中定义一个属性即可


下面来看代码:
AdressBook.h
[objc]  view plaincopy
1. //  
2. //  AdressBook.h  
3. //  35_Singleton  
4. //  
5. //  Created by jiangwei on 14-10-13.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import <Foundation/Foundation.h>  
10.   
11. //设计单利类的目的,限制这个类只能创建一个对象  
12.   
13. //构造方法为私有的  
14. //保存一个全局的static变量  
15. @interface AdressBook : NSObject  
16.   
17. + (AdressBook *)shareInstance;  
18.   
19. @end  
在.h文件中提供了一个类方法,用于产生对象的


AdressBook.m
[objc]  view plaincopy
1. //  
2. //  AdressBook.m  
3. //  35_Singleton  
4. //  
5. //  Created by jiangwei on 14-10-13.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import "AdressBook.h"  
10.   
11. static AdressBook *instance = nil;//不能让外部访问,同时放在静态块中的  
12.   
13. @implementation AdressBook  
14.   
15. + (AdressBook *)shareInstance{  
16.     if(instance == nil){  
17.         instance = [[AdressBook alloc] init];  
18.     }  
19.     return instance;  
20. }  
21.   
22. //限制方法,类只能初始化一次  
23. //alloc的时候调用  
24. + (id) allocWithZone:(struct _NSZone *)zone{  
25.     if(instance == nil){  
26.         instance = [super allocWithZone:zone];  
27.     }  
28.     return instance;  
29. }  
30.   
31. //拷贝方法  
32. - (id)copyWithZone:(NSZone *)zone{  
33.     return instance;  
34. }  
35.   
36. //需要重写release方法,不能让其引用+1  
37. - (id)retain{  
38.     return self;  
39. }  
40.   
41. //需要重写release方法,不能让其引用-1  
42. - (oneway void)release{  
43.     //do Nothing...  
44. }  
45.   
46. - (id)autorelease{  
47.     return self;  
48. }  
49.   
50. @end  


我们定义了一个static类型的对象
[java]  view plaincopy
1. static AdressBook *instance = nil;//不能让外部访问,同时放在静态块中的  


提供一个获取单实例的类方法
[objc]  view plaincopy
1. + (AdressBook *)shareInstance{  
2.     if(instance == nil){  
3.         instance = [[AdressBook alloc] init];  
4.     }  
5.     return instance;  
6. }  

重写allocWithZone方法,让初始化只做一次

[objc]  view plaincopy
1. //限制方法,类只能初始化一次  
2. //alloc的时候调用  
3. + (id) allocWithZone:(struct _NSZone *)zone{  
4.     if(instance == nil){  
5.         instance = [super allocWithZone:zone];  
6.     }  
7.     return instance;  
8. }  

还有其他方法也是需要重写的
[objc]  view plaincopy
1. //拷贝方法  
2. - (id)copyWithZone:(NSZone *)zone{  
3.     return instance;  
4. }  
5.   
6. //需要重写release方法,不能让其引用+1  
7. - (id)retain{  
8.     return self;  
9. }  
10.   
11. //需要重写release方法,不能让其引用-1  
12. - (oneway void)release{  
13.     //do Nothing...  
14. }  
15.   
16. - (id)autorelease{  
17.     return self;  
18. }  
拷贝方法只能返回当前的单实例
retain和release方法不能进行引用的+1和-1操作


测试代码
main.m
[objc]  view plaincopy
1. //  
2. //  main.m  
3. //  35_Singleton  
4. //  
5. //  Created by jiangwei on 14-10-13.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import <Foundation/Foundation.h>  
10.   
11. #import "AdressBook.h"  
12.   
13. //单利模式  
14. int main(int argc, const charchar * argv[]) {  
15.     @autoreleasepool {  
16.           
17.         AdressBook *book1 = [AdressBook shareInstance];  
18.         AdressBook *book2 = [AdressBook shareInstance];  
19.           
20.         NSLog(@"%@",book1);  
21.         NSLog(@"%@",book2);  
22.           
23.     }  
24.     return 0;  
25. }  
两个指针指向的是同一个对象


总结
这一篇文章主要介绍了OC中的单例模式,同时我们OC学习篇的系列章程也就结束了,当然这些并不代表OC中所有的内容,还有很多其他内容,我们只有后面慢慢的去使用他才会越来越了解。
posted @ 2016-01-06 14:28  kunzir  阅读(134)  评论(0编辑  收藏  举报