objective-c 單例模式 singleton

來源:互聯網
上載者:User

標籤:

先把參考的文檔列出來:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.