日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

iOS设计模式之单例模式

發布時間:2025/3/15 asp.net 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS设计模式之单例模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? 單例模式是iOS常用設計模式中的一種。單例設計模式的作用是使得這個類的一個對象成為系統中的唯一實例,因此需要用一種唯一的方法去創建這個對象并返回這個對象的地址。那么,我們何時使用單例模式呢?1、類只能有一個實例,而且必須從一個為人熟知的訪問點對其訪問。2、這個唯一的實例只能通過子類化進行擴展,而且擴展的對象不會破壞客戶端代碼。

? ? 那么用Objective-C如何實現單例模式呢?下面我們來新建一個Singleton類,在Singleton.h中實現如下

?

[objc]?view plaincopy
  • @interface?Singleton?:?NSObject??
  • ??
  • +?(Singleton?*)?sharedInstance;??
  • ??
  • @end??
  • ?

    ?

    在Singleton.m

    [objc]?view plaincopy
  • @implementation?Singleton??
  • ??
  • static?Singleton?*?sharedSingleton?=?nil;??
  • ??
  • +?(Singleton?*)?sharedInstance??
  • {??
  • ????if?(sharedSingleton?==?nil)?{??
  • ????????sharedSingleton?=?[[Singleton?alloc]?init];??
  • ????}??
  • ????return?sharedSingleton;??
  • }??
  • ??
  • @end??

  • 這樣就創建一個簡單的單例模式,實際上有一部分程序員也是這樣實現的,但實際上這是一個不“嚴格”版本,在實際中使用,可能會遇到發起調用的對象不能以其他分配方式實例化單例對象,否則,就會創建多個實例。(之前有人和我討論過這個問題,說使用者應該嚴格按照接口來使用,當實際上Singleton是一個對象,我們不能保證使用者不會使用其他的方法去創建(比如alloc),這個時候他就會創建多個實例,這樣就會出現這些無法感知的bug)

    ?

    ?

    下面我對Singleton.m的進行改進

    [objc]?view plaincopy
  • @implementation?Singleton??
  • ??
  • static?Singleton?*?sharedSingleton?=?nil;??
  • ??
  • +?(Singleton?*)?sharedInstance??
  • {??
  • ????if?(sharedSingleton?==?nil)?{??
  • ????????sharedSingleton?=?[[super?allocWithZone:NULL]?init];??
  • ????}??
  • ????return?sharedSingleton;??
  • }??
  • ??
  • +?(id)?allocWithZone:(struct?_NSZone?*)zone??
  • {??
  • ????return?[[self?sharedInstance]?retain];??
  • }??
  • ??
  • -?(id)?copyWithZone:(NSZone?*)?zone??
  • {??
  • ????return?self;??
  • }??
  • ??
  • -?(id)?retain??
  • {??
  • ????return?self;??
  • }??
  • ??
  • -?(NSUInteger)?retainCount??
  • {??
  • ????return?NSUIntegerMax;??
  • }??
  • ??
  • ??
  • -?(void)?release??
  • {??
  • ????//??
  • }??
  • ??
  • -?(id)?autorelease??
  • {??
  • ????return?self;??
  • }??
  • ??
  • @end??

  • 也許你注意到了,我重載了allocWithZone:,保持了從sharedInstance方法返回的單例對象,使用者哪怕使用alloc時也會返回唯一的實例(alloc方法中會先調用allocWithZone:創建對象)。而retain等內存管理的函數也被重載了,這樣做讓我們有了把Singleton類變得“嚴格”了。

    轉載于:https://www.cnblogs.com/yueyingsuixing/p/3457725.html

    總結

    以上是生活随笔為你收集整理的iOS设计模式之单例模式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。