iOS的單例模式


IOS中的單例模式
  在objective-c中要實現一個單例類,至少需要做以下四個步驟:
  1、為單例對象實現一個靜態實例,並初始化,然后設置成nil,
  2、實現一個實例構造方法檢查上面聲明的靜態實例是否為nil,如果是則新建並返回一個本類的實例,
  3、重寫allocWithZone方法,用來保證其他人直接使用alloc和init試圖獲得一個新實力的時候不產生一個新實例,
  4、適當實現allocWitheZone,copyWithZone,release和autorelease。
下面以SurveyRunTimeData為例子:
 
static SurveyRunTimeData *sharedObj = nil; //第一步:靜態實例,並初始化。

@implementation SurveyRunTimeData

+ (SurveyRunTimeData*) sharedInstance  //第二步:實例構造檢查靜態實例是否為nil
{
    @synchronized (self)
    {
        if (sharedObj == nil)
        {
            sharedObj = [[self alloc] init];
        }
    }
    return sharedObj;
}

+ (id) allocWithZone:(NSZone *)zone //第三步:重寫allocWithZone方法
{
    @synchronized (self) {
        if (sharedObj == nil) {
            sharedObj = [super allocWithZone:zone];
            return sharedObj;
        }
    }
    return nil;
}

- (id) copyWithZone:(NSZone *)zone //第四步
{
    return self;
}

- (id) retain
{
    return self;
}

- (unsigned) retainCount
{
    return UINT_MAX;
}

- (oneway void) release
{
    
}

- (id) autorelease
{
    return self;
}

- (id)init
{
    @synchronized(self) {
        [super init];//往往放一些要初始化的變量.
        return self;
    }
}

@end

 

單例是指靜態分配的實例,而 iphone sdk 中全是這種實例,例如

[UIApplication sharedApplication] 返回一個指向代表應用程序的單例對象的指針。[UIDevice currentDevice] 獲取一個代表所有使用硬件平台的對象。

將類方法與單例相結合,便可以在程序的任何地方訪問靜態實例,而無需使用指向對象的指針或保存它的實例變量。創建類的唯一實例(普通單例)的函數示例:

//在很多時候,我們使用某個類的唯一實例。最常見的就是一個程序的主類,以下是以名為 RootViewController 創建的一個單例函數:

static RootViewController *sharedRootController = nil;

+(RootViewController *) sharedController{ 
 
  @synchronized(self)
  {     
     if (sharedRootController == nil)
     {          
        sharedRootController = [[[self alloc] init] autorelease];       
     }   
  }
  return sharedRootController; 
}

+(id) allocWithZone:(NSZone *)zone
{   
   @synchronized(self)
   {       
      if (sharedRootController == nil)
     {   
         sharedRootController = [super allocWithZone:zone];     
         return sharedRootController;      
     }
   }   
   return nil; 
}

代碼說明: 

1、synchronized 這個主要是考慮多線程的程序,這個指令可以將{ } 內的代碼限制在一個線程執行,如果某個線程沒有執行完,其他的線程如果需要執行就得等着。 

2、網上搜索的代碼,好像有一個沒有加入 autorelease,我覺得應該需要加。因為如果調用的函數沒有release就麻煩了(我覺得,iOS 上的程序,對於創建用於函數返回值的,都應該考慮 autorelease)。

3、allocWithZone 這個是重載的,因為這個是從制定的內存區域讀取信息創建實例,所以如果需要的單例已經有了,就需要禁止修改當前單例,所以返回 nil


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM