先把参考的文档列出来:
http://www.cnblogs.com/supercheng/archive/2012/11/26/singlemodal.html
http://arthurchen.blog.51cto.com/2483760/642536/
http://blog.csdn.net/duboleon/article/details/6337174
https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Singleton.html
《大话设计模式》
http://www.cocoachina.com/industry/20130510/6168.html
===============================================================================
singleton 单例模式 :保证一个类仅有一个实例,并提供一个访问它的全局访问点。
实现单例模式需要重点考虑的是 多线程访问 引起的创建多个实例的情况。
未考虑多线程的代码如下:
// // myManager.h // singleton // // Created by 一棋王 on 15-3-7. // Copyright (c) 2015年 一棋王. All rights reserved. // #import <Foundation/Foundation.h> @interface myManager : NSObject{ NSString * _namer; } @property(nonatomic,retain) NSString * name; +(id) sharedMyManager; //sharedXXX,defaultXXX,currentXXX @end CarryOn 2015/3/10 19:44:49 // // myManager.m // singleton // // Created by 一棋王 on 15-3-7. // Copyright (c) 2015年 一棋王. All rights reserved. // #import "myManager.h" // 创建单例对象 static myManager * instance; @implementation myManager @synthesize name = _name; +(id)sharedMyManager{ if(instance == nil){ instance = [[self alloc]init]; } return instance; } @end // // main.m // singleton // // Created by 一棋王 on 15-3-7. // Copyright (c) 2015年 一棋王. All rights reserved. // #import <Foundation/Foundation.h> #import "myManager.h" int main(int argc, const char * argv[]) { @autoreleasepool { myManager *tempObject1 = [myManager sharedMyManager]; myManager *tempObject2 = [myManager sharedMyManager]; NSLog(@"tempObject1 is %p",tempObject1); NSLog(@"tempObject2 is %p",tempObject2); } return 0; } //output 2015-03-07 00:51:03.865 singleton[752:303] tempObject1 is 0x100601c20 2015-03-07 00:51:03.871 singleton[752:303] tempObject2 is 0x100601c20 Program ended with exit code: 0
考虑多线程的代码,即推荐代码:
// // myManager.h // singleton // // Created by 一棋王 on 15-3-7. // Copyright (c) 2015年 一棋王. All rights reserved. // #import <Foundation/Foundation.h> @interface myManager : NSObject{ NSString * _namer; } @property(nonatomic,retain) NSString * name; +(id) sharedMyManager; //sharedXXX,defaultXXX,currentXXX +(id)allocWithZone:(NSZone *)zone; -(id)copyWithZone:(NSZone *)zone; -(id)retain; -(NSUInteger)retainCount; -(id)autorelease; -(oneway void)release; @end // // myManager.m // singleton // // Created by 一棋王 on 15-3-7. // Copyright (c) 2015年 一棋王. All rights reserved. // #import "myManager.h" // 创建单例对象 static myManager * instance; @implementation myManager @synthesize name = _name; +(id)sharedMyManager{ @synchronized(self){//synchronized关键字确保该段代码同时只有一个线程访问 if(instance == nil){ instance = [[self alloc]init]; } } return instance; } +(id)allocWithZone:(NSZone *)zone{ @synchronized(self){ if(instance == nil){ instance = [super allocWithZone:zone]; return instance; } return nil; } } -(id)copyWithZone:(NSZone *)zone{ return self; //确保copy对象唯一。 } -(id)retain{ return self; //确保计数唯一 } -(NSUInteger)retainCount{ return UINT_MAX; } -(id)autorelease{ return self; } -(oneway void)release{ //do nothing } @end //还有一种GCD写法/*+(id)sharedMyManager{ static dispatch_once_t once;//保证once只使用一次 dispatch_once(&once,^{ if(instance == null){ instance = [[self alloc]init]; }) return instance;}*/
重写这些方法是为了采用其他方法声明实例时,也能遵守单例规则,即只存在一个对象。
时间: 2024-10-12 22:57:56