在設計模式中有一個“單例模式”,對應的我們常常會設計“單例類”(或稱單件類)。但在實際應用中,我們常常也需要使用“半單例”。下面我們具體談談單例和半單例,以及他們的用法和區別。單例模式單例模式(singleton)顧名思義,就是只有一個執行個體。作為對象的建立模式[GOF95], 單例模式確保某一個類只有一個執行個體,而且自行執行個體化並向整個系統提供這個執行個體。這個類稱為單例類。也就是說,對於一個單例類,不論執行個體化對象多少次,都只有一個對象執行個體,而且這個執行個體一定是一個全域的能被整個系統訪問到。下面是Objective-C中完整的單例類的實現:Singleton.h#import <Foundation/Foundation.h> @interface Singleton : NSObject +(id)shareInstance; @end Singleton.m#import "Singleton.h"static Singleton * instance = nil;@implementation Singleton +(id)shareInstance{ if(instance == nil) { instance = [[super allocWithZone:nil]init]; //super 調用allocWithZone } return instance;} +(id)allocWithZone:(NSZone *)zone{ return [Singleton shareInstance]; } //可寫可不寫- (id)init { if (instance) { return instance; } self = [super init]; return self;} -(id)copy{ return self;}- (id)copyWithZone:(NSZone *)zone{ return self;}-(id)retain{ return self;}- (oneway void)release { // Do nothing} - (id)autorelease { return self;} - (NSUInteger)retainCount { return NSUIntegerMax;}@end 解釋說明:1.static Singleton * instance = nil;靜態全域變數,始終指向執行個體化出的對象。2.+(id)shareInstance;外界初始化得到單例類對象的唯一借口,這個類方法返回的就是instance,即類的一個對象,如果instance為空白,則執行個體化一個對象,如果不為空白,則直接返回。這樣保證了執行個體的唯一。3.-(id)copy;- (id)copyWithZone:(NSZone *)zone;這兩個方法是為了防止外界拷貝造成多個執行個體,保證執行個體的唯一性。4.-(id)retain;因為只有一個執行個體對象,所以retain不能增加引用計數。5.- (NSUInteger)retainCount;因為只有一個執行個體對象,設定預設引用計數。這裡是取的NSUinteger的最大值,當然也可以設定成1或其他值。6.- (onewayvoid)release;oneway void是用於多線程編程中,表示單向執行,不能“復原”,即原子操作。 半單例1.半單例不同於單例,它可以執行個體化多個對象。2.在程式中系統要求能訪問到這個類的當前對象執行個體。比如說:我們對於一個ViewController,我們可以執行個體化多個。在ViewController中,我們給他添加了很多視圖View。這些View中,當與使用者發生某個互動時,我們由需要向Controller發送訊息,實現響應操作。那麼,View必須能找到當前的ViewController。這時,我們可以將ViewController設定成一個半單例類。 以“翻書”程式為例:這裡涉及到兩個類LeavesViewController 和LeavesView顯然,我們是在LeavesViewController中添加多個LeavesView實現多頁效果,當判斷出LeavesView翻到最後一頁時,我們需要讓LeavesViewController響應,跳到下一個Controller,其他情境視圖。LeavesViewController類為“半單例”:LeavesViewController.h#import <UIKit/UIKit.h>#import "LeavesView.h" @interface LeavesViewController : UIViewController <LeavesViewDataSource, LeavesViewDelegate> {LeavesView *leavesView;}@property(nonatomic,retain)LeavesView *leavesView; + (id)shareInstance; - (void)goToPlay;@end LeavesViewController.m#import "LeavesViewController.h"#import "ASCcLevelOnePaperScene.h" static LeavesViewController *leavesViewInstance = nil; @implementation LeavesViewController @synthesize leavesView;- (id)init { if (self = [super init]) { leavesView = [[LeavesView alloc] initWithFrame:CGRectZero]; leavesView.mode = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? LeavesViewModeSinglePage : LeavesViewModeFacingPages; leavesViewInstance = self; //注意這裡 } return self;} - (void)dealloc { [leavesView release]; leavesViewInstance = nil; //釋放全域變數 [super dealloc];} + (id)shareInstance{ //NSAssert(leavesViewInstance!=nil,@"leavesViewInstance can not be nil!"); if(leavesViewInstance == nil) leavesViewInstance = [self init]; return leavesViewInstance;} 這裡只展示了“半單例”的實現部分,關於Controller都有的ViewDidLoad等方法和其他相關實現方法,這裡與“半單例”無關,不做展示LeavesView只是一個普通的視圖類。當LeavesView判斷到最後一頁時:if([self hasNextPage]==NO){ NSLog(@"最後一頁!"); [[LeavesViewController shareInstance] goToPlay];} [LeavesViewControllershareInstance]得到當前ViewController。再發送訊息goToPlay,讓ViewController響應。