標籤:
先把參考的文檔列出來:
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@endCarryOn 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;}//output2015-03-07 00:51:03.865 singleton[752:303] tempObject1 is 0x100601c202015-03-07 00:51:03.871 singleton[752:303] tempObject2 is 0x100601c20Program 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;
}
*/
重寫這些方法是為了採用其他方法聲明執行個體時,也能遵守單例規則,即只存在一個對象。
objective-c 單例模式 singleton