簡單描述下對單利模式設計的理解
節(jié)省內(nèi)存資源,一個應用就一個對象炭玫。
- 單例設計模式(Singleton): 它可以保證某個類創(chuàng)建出來的對象永遠只有1個
- 作用: 如果有一些數(shù)據(jù), 整個程序中都用得上, 只需要使用同一份資源(保證大家訪問的數(shù)據(jù)是相同的,一致的)奈嘿,例如UIApplication,NSUserDefaults,UIDevice,NSFileManager等等;
一般來說,工具類設計為單例模式比較合適吞加,節(jié)省內(nèi)存開銷 - 怎么實現(xiàn)
+ (instancetype)sharedInstance
{
static dispatch_once_t once;
static id sharedInstance;
dispatch_once(&once,^{
sharedInstance = [ [self alloc] init];
});
return sharedInstance;
}