
設(shè)計(jì)模式分為三大類:創(chuàng)建型、結(jié)構(gòu)性、行為型
1、為什么用單例模式?
OC編程習(xí)慣(Java編程習(xí)慣、Swift編程習(xí)慣)
xcode 4.2之前是MRC(手動(dòng)分配和釋放內(nèi)存)
alloc:開(kāi)辟內(nèi)存? release:釋放內(nèi)存
開(kāi)發(fā)者有的時(shí)候很容忽略釋放
xcode 4.2之后ARC(自動(dòng)引用計(jì)數(shù))
OOM:內(nèi)存溢出
有的時(shí)候整個(gè)程序中為了避免創(chuàng)建多個(gè)對(duì)象,引入了單例模式
UIApplication
2、單例模式使用?
2.1 單例模式-特點(diǎn)
第一個(gè)特點(diǎn):有且只有一個(gè)實(shí)例(全局唯一)
第二個(gè)特點(diǎn):必須自行創(chuàng)建一個(gè)實(shí)例
第三個(gè)特點(diǎn):必須提供一個(gè)全局實(shí)例,暴露給外部使用
2.2 單例模式-角色劃分
角色:?jiǎn)卫悺⒖蛻舳耍ㄊ褂谜撸?/p>
2.3 單例模式-如何實(shí)現(xiàn)?
約束:
約束一:提供一個(gè)靜態(tài)實(shí)例,一般情況下設(shè)置為nil
擴(kuò)展:在Swift、Java中也是如此
約束二:提供一個(gè)方法創(chuàng)建單例,如果單例存在我就返回,如果不存在我就創(chuàng)建
約束三:在OC里面重寫(xiě)父類中的allocWithZone方法,保證是一個(gè)單例
當(dāng)我們?cè)谡{(diào)用alloc的時(shí)候回調(diào)改方法
約束四:OC需要重寫(xiě)父類copyWithzone等等...... (Swift中你要將構(gòu)造方法私有化、Java里面也是如此構(gòu)造方法私有化)
2.3.1 實(shí)現(xiàn)OC-單例模式(詳細(xì)實(shí)現(xiàn)代碼見(jiàn)最后)
線程安全和非線程安全
案例一和案例二都:懶漢式(等你需要的時(shí)候創(chuàng)建)
案例一:標(biāo)準(zhǔn)單例(非線程安全)
案例二:標(biāo)準(zhǔn)單例(GCD實(shí)現(xiàn))(線程安全)
案例三:餓漢式(不管你要不要我都給你)
案例三:實(shí)現(xiàn)餓漢式
注意:load方法(當(dāng)我們應(yīng)用程序運(yùn)行時(shí)候,回調(diào)改方法)
案例四:@synchronized實(shí)現(xiàn)
2.3.2 實(shí)現(xiàn)Swift-單例模式(詳細(xì)實(shí)現(xiàn)代碼見(jiàn)最后)
案例一:標(biāo)準(zhǔn)單例(所謂單例模式:不能夠被繼承(可選))
在我們的Swift中是可以的(final關(guān)鍵字)
(非線程安全)
案例二:標(biāo)準(zhǔn)寫(xiě)法(靜態(tài)屬性實(shí)例化:線程安全)
案例三:標(biāo)準(zhǔn)寫(xiě)法(結(jié)構(gòu)體實(shí)現(xiàn)單例:線程安全)
架構(gòu)師:鍛煉你的思想(相通性)
2.3.3 分享(擴(kuò)展)實(shí)現(xiàn)Java-單例模式
java中單例寫(xiě)法基本上和Swift差不多
案例一:標(biāo)準(zhǔn)單例模式(非線程安全)
案例二:標(biāo)準(zhǔn)單例模式(線程安全)
案例三:枚舉定義單例(java特效:枚舉只能夠?qū)嵗淮危?/p>
java中的枚舉高級(jí)編程
很多單例的寫(xiě)法我就不一一列舉
Java美圖秀秀(美白:例如給1080x1920,大概需要15秒鐘才能夠搞定)
但是Java有解決方案:NDK開(kāi)發(fā),基于C/C++開(kāi)發(fā)(0.5秒美白)
回到我們的OC和Swift性能比Java搞很多倍,不需要質(zhì)疑(大概也就是1秒左右)
3、單例模式-UML類圖結(jié)構(gòu):依賴關(guān)系
客戶端類:持有單例對(duì)象引用
4、單例模式-使用場(chǎng)景?
iOS系統(tǒng)中使用場(chǎng)景
UIApplication:?jiǎn)卫?/p>
NSNotificationCenter:?jiǎn)卫J?,但是不是最為?biāo)準(zhǔn)單例
NSFileManager:?jiǎn)卫J?/p>
NSUserDefaults:?jiǎn)卫J?/p>
NSURLCache:?jiǎn)卫J?/p>
實(shí)際開(kāi)發(fā):工具類(單例、數(shù)據(jù)庫(kù)、圖片下載等等......)
OC:
案例一(.m實(shí)現(xiàn)文件代碼)
static Singleton01 *singleton = nil;
+ (instancetype)ShareInstance{
if (singleton == nil) {
singleton = [[Singleton01 alloc]init];
}
return singleton;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (singleton == nil) {
singleton = [super allocWithZone:zone];
}
return singleton;
}
案例二(.m實(shí)現(xiàn)文件代碼)
static Singleton02 *instance = nil;
+ (instancetype)ShareInstance{
if(instance == nil){
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
instance = [[Singleton02 alloc]init];
});
}
return instance;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (instance == nil) {
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
instance = [super allocWithZone:zone];
});
}
return instance;
}
案例三(.m實(shí)現(xiàn)文件代碼)
static Singleton03 *instance = nil;
+ (void)load{
//設(shè)置斷點(diǎn)測(cè)試
[Singleton03 ShareInstance];
}
+ (instancetype)ShareInstance{
if (instance == nil) {
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
instance = [[Singleton03 alloc]init];
});
}
return instance;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (instance == nil) {
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
instance = [super allocWithZone:zone];
});
}
return instance;
}
案例四(.m實(shí)現(xiàn)文件代碼)
static Singleton04 *instance;
+ (instancetype)ShareInstance{
@synchronized(self) {
if (instance == nil) {
instance = [[Singleton04 alloc]init];
}
}
return instance;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (instance == nil) {
@synchronized(self) {
if (instance == nil) {
instance = [super allocWithZone:zone];
}
}
}
return instance;
}
Swift:
案例一
final class Singleton01: NSObject {
private static var instance:Singleton01? = nil
class func ShareInstance() -> Singleton01 {
if(instance == nil){
instance = Singleton01()
}
return instance!
}
private override init() {
}
}
案例二
final class Singleton02: NSObject {
private static let instance = Singleton02()
class func ShareInstance() -> Singleton02 {
return instance;
}
private override init() {
}
}
案例三
final class Singleton03: NSObject {
private class var shared: Singleton03{
struct Static {
static let instance = Singleton03()
}
return Static.instance
}
class func ShareInstance() -> Singleton03 {
return shared;
}
private override init() {
}
}
Java:代碼待續(xù)(可惜只會(huì)iOS開(kāi)發(fā),安卓單例據(jù)說(shuō)20幾種實(shí)現(xiàn)方式,一般用枚舉實(shí)現(xiàn),后續(xù)學(xué)習(xí)了Java后會(huì)繼續(xù)分享源碼,)