Singleton パターン
コーディング時に大枠を何処かからコピーペーストして修正実装
するパターンがよくあると思います。
実装時、コピペ用のテンプレートとしてご利用ください。
コードサンプル
■ シングルトンを取得して、後はお好きに。
TestClass *testClass = [[TestClass getSharedInstance];
■ シングルトンクラス
//--------------------------------------------------------------------------------------------------------------------
@interface TestClass : NSObject
+ (ManageDatas*)getSharedInstance;
@end
//--------------------------------------------------------------------------------------------------------------------
@implementation TestClass
// モダンなシングルトン
+ (TestClass *)getSharedInstance {
static dispatch_once_t pred;
static TestClass *_sharedInstance = nil;
dispatch_once(&pred, ^{
_sharedInstance = [[TestClass alloc] init];
});
return _sharedInstance;
}
// 昔ながらのシングルトン
TestClass *testClass = [[TestClass getSharedInstance];
static TestClass *_sharedInstance = nil;
+ (TestClass*)getSharedInstance
{
if(!_sharedInstance){
_sharedInstance = [[TestClass alloc] init];
}
return _sharedInstance;
}
@end

