工廠模式就是我們?cè)趧?chuàng)建對(duì)象時(shí)不會(huì)對(duì)客戶端暴露創(chuàng)建邏輯,并且是通過(guò)使用一個(gè)共同的接口來(lái)指向新創(chuàng)建的對(duì)象。
工廠模式又分為簡(jiǎn)單工廠模式,工廠方法模式,抽象工廠模式。
下面的三張圖,完美的解釋了什么是工廠模式。
簡(jiǎn)單工廠模式

1419489-20190628144601084-563759643.png
工廠方法模式

1419489-20190628154133368-906051111.png
抽象工廠模式

1419489-20190628170705865-1781414242.png
工廠模式優(yōu)點(diǎn)
一個(gè)調(diào)用者想創(chuàng)建一個(gè)對(duì)象,只要知道其名稱(chēng)就可以了。
擴(kuò)展性高,如果想增加一個(gè)產(chǎn)品,只要擴(kuò)展一個(gè)工廠類(lèi)就可以。
屏蔽產(chǎn)品的具體實(shí)現(xiàn),調(diào)用者只關(guān)心產(chǎn)品的接口。
工廠模式缺點(diǎn)
每次增加一個(gè)產(chǎn)品時(shí),都需要增加一個(gè)具體類(lèi)和對(duì)象實(shí)現(xiàn)工廠,使得系統(tǒng)中類(lèi)的個(gè)數(shù)成倍增加,在一定程度上增加了系統(tǒng)的復(fù)雜度,同時(shí)也增加了系統(tǒng)具體類(lèi)的依賴。這并不是什么好事。
其實(shí)如果懂了抽象工廠模式,上面的兩種模式也就都懂了。所以看下面這個(gè)例子。
現(xiàn)在我們要?jiǎng)?chuàng)建兩個(gè)工廠,一個(gè)是蘋(píng)果廠,一個(gè)是谷歌廠,同時(shí)蘋(píng)果廠可以生產(chǎn)蘋(píng)果手機(jī)和蘋(píng)果手表,谷歌廠可以生產(chǎn)安卓手機(jī)和安卓手表,同時(shí)每部手機(jī)不光可以打電話,發(fā)短信,還都有自己獨(dú)特的功能,蘋(píng)果手機(jī)可以指紋識(shí)別,安卓可以主題定制。
需要先創(chuàng)建工廠基類(lèi)。
@implementation BaseFactory
- (BasePhone *)createPhone {
return nil;
}
- (BaseWatch *)createWatch {
return nil;
}
@end
然后根據(jù)基類(lèi)分別創(chuàng)建蘋(píng)果廠和谷歌廠。
@implementation AppleFactory
- (BasePhone *)createPhone {
return [[IPhone alloc] init];
}
- (BaseWatch *)createWatch {
return [[IWatch alloc] init];
}
@end
@implementation GoogleFactory
- (BasePhone *)createPhone {
return [[Android alloc] init];
}
- (BaseWatch *)createWatch {
return [[AndroidWatch alloc] init];
}
@end
然后創(chuàng)建手機(jī)基類(lèi)。
@interface BasePhone : NSObject <PhoneProtocol>
@end
@implementation BasePhone
- (void)phoneCall {
}
- (void)sendMessage {
}
@end
根據(jù)手機(jī)基類(lèi)創(chuàng)建不同的手機(jī)。
@implementation IPhone
- (void)phoneCall {
NSLog(@"iPhone phoneCall");
}
- (void)sendMessage {
NSLog(@"iPhone sendMessage");
}
- (void)fingerprintIndetification {
NSLog(@"iPhone fingerprintIndetification");
}
@end
@implementation Android
- (void)phoneCall {
NSLog(@"Android phoneCall");
}
- (void)sendMessage {
NSLog(@"Android sendMessage");
}
- (void)customTheme {
NSLog(@"Android customTheme");
}
@end
創(chuàng)建不同工廠的工廠管理類(lèi)。
typedef NS_ENUM(NSUInteger, KFactoryType) {
KApple,
KGoogle
};
@interface FactoryManager : NSObject
/**
獲取工廠
@param factoryType 工廠類(lèi)型
@return 創(chuàng)建出的工廠
*/
+ (BaseFactory *)factoryWithBrand:(KFactoryType)factoryType;
@end
+ (BaseFactory *)factoryWithBrand:(KFactoryType)factoryType {
BaseFactory *factory = nil;
if (factoryType == KApple) {
factory = [[AppleFactory alloc] init];
} else if (factoryType == KGoogle) {
factory = [[GoogleFactory alloc] init];
}
return factory;
}
@end
那么下面就是來(lái)使用了,屏蔽內(nèi)部實(shí)現(xiàn),通過(guò)不同工廠類(lèi)組裝成的抽象工廠模式
// 獲取工廠
BaseFactory *googleFactory = [FactoryManager factoryWithBrand:KGoogle];
// 創(chuàng)建商品
Android *androidPhone = (Android *)[googleFactory createPhone];
BaseWatch *androidWatch = [googleFactory createWatch];
[androidPhone phoneCall];
//定制主題
[androidPhone customTheme];
// 獲取工廠
BaseFactory *appleFactory = [FactoryManager factoryWithBrand:KApple];
// 創(chuàng)建商品
IPhone *applePhone = (IPhone *)[appleFactory createPhone];
BaseWatch *appleWatch = [appleFactory createWatch];
[applePhone phoneCall];
//指紋識(shí)別
[applePhone fingerprintIndetification];