单例模式的创建

在之前的一片文章中介绍了对象的拷贝相关知识:http://blog.csdn.net/jiangwei0910410003/article/details/41926531,今天我们来看一下OC中的单例模式,单例模式在设计模式中用的可能是最多的一种了,而且也是最简单的一种

实现单例模式有三个条件

1、类的构造方法是私有的

2、类提供一个类方法用于产生对象

3、类中有一个私有的自己对象


针对于这三个条件,OC中都是可以做到的

1、类的构造方法是私有的

我们只需要重写allocWithZone方法,让初始化操作只执行一次

2、类提供一个类方法产生对象

这个可以直接定义一个类方法

3、类中有一个私有的自己对象

我们可以在.m文件中定义一个属性即可


下面来看代码:

AdressBook.h

  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

  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类型的对象

  1. static AdressBook *instance = nil;//不能让外部访问,同时放在静态块中的  


提供一个获取单实例的类方法

  1. + (AdressBook *)shareInstance{  
  2.     if(instance == nil){  
  3.         instance = [[AdressBook alloc] init];  
  4.     }  
  5.     return instance;  
  6. }  

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

还有其他方法也是需要重写的

  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

  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中所有的内容,还有很多其他内容,我们只有后面慢慢的去使用他才会越来越了解。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值