工廠模式算是開發中比較常見的設計模式,簡單工廠模式,工廠模式和抽象工廠模式,都屬於工廠模式。簡單工廠模式(simple factory)是類的創建模式,靜態工廠方法(static factory method)模式,簡單工廠模式就是由一個工廠類根據傳入的參數決定創建哪一種的產品類。簡單工廠模式會包含過多的判斷條件,維護起來不是特別方便,工廠模式是主要通過依賴倒置將類的實例化推遲到子類中,實現動態擴展。抽象工廠模式是一個對象產品家族,根據需求提供不同的對象。
簡單工廠模式
之前的文章中寫過一篇簡單工廠模式,假設我們我有一個服裝加工工廠,可以根據不同的需求生產不同的服飾,定義一個服裝基類和工廠類:
@protocol DressProtocol <NSObject> @optional -(void)provideMaterial; @optional -(void)product; @end @interface Dress : NSObject<DressProtocol> @end
服裝Dress子類ForeignDress:
@implementation ForeignDress
-(void)provideMaterial{
NSLog(@"ForeignDress--准備原材料");
}
-(void)product{
NSLog(@"ForeignDress--生產");
}
@end
ChinaDress子類:
@implementation ChinaDress
-(void)provideMaterial{
NSLog(@"ChinaDress---准備原材料");
}
-(void)product{
NSLog(@"ChinaDress---生產");
}
@end
工廠類Manufactor:
@protocol ManufactorProtocol <NSObject> @optional -(Dress *)createDress:(NSString *)type; @end @interface Manufactor : NSObject<ManufactorProtocol> -(void)start:(NSString *)type; -(void)simpleStart:(NSString *)type; -(void)startByCondition:(NSString *)type; @end
方法實現:
-(void)start:(NSString *)type{
if ([type isEqualToString:@"Foreign"]) {
dress=[[ForeignDress alloc]init];
}else if([type isEqualToString:@"China"]){
dress=[[ChinaDress alloc]init];
}
[dress provideMaterial];
[dress product];
}
//博客園-FlyElephant 簡單工廠
-(void)simpleStart:(NSString *)type{
dress=[ManuFactory dressInstance:type];
[dress provideMaterial];
[dress product];
}
方法調用:
Manufactor *factor=[[Manufactor alloc]init];
[factor start:@"Foreign"];
[factor simpleStart:@"China"];
NSLog(@"博客園-FlyElephant");
NSLog(@"http://www.cnblogs.com/xiaofeixiang/");
測試效果:

第一種我們在工廠中直接通過type類型判斷,不同的類型生產不同的服裝,但是如果type類型過多而且type實現的不一定的Dress服裝類,所以放在Manufactor不合適,我們將其實現單獨放在一個簡單工廠里面。 你會發現,每次不同的類型我們都要去修改簡單工廠,牽一發而動,不符合設計模式的"對擴展開放,對修改關閉"的原則,工廠模式可以解決簡單工廠無法解決的問題。
工廠模式
@implementation BJManufactor
-(Dress *)createDress:(NSString *)type{
Dress *dress;
if ([type isEqualToString:@"BJ"]) {
dress=[[BJDress alloc]init];
}else if([type isEqualToString:@"BJSpecial"]){
dress=[[BJSpecialDress alloc]init];
}
return dress;
}
@end
Manufactor基類中的條件判斷:
-(void)startByCondition:(NSString *)type{
Dress *myDress=[self createDress:type];
[myDress provideMaterial];
[myDress product];
}
具體調用實現:
Manufactor *bjfactor=[[BJManufactor alloc]init];
[bjfactor startByCondition:@"BJ"];
NSLog(@"博客園-FlyElephant");
NSLog(@"http://www.cnblogs.com/xiaofeixiang/");
效果:

抽象工廠模式
抽象工廠基類,這里只提供實現了Cloth:
@protocol AbstractFactory <NSObject> @optional -(Cloth *)provideCloth; @end @interface AbstractFactory : NSObject<AbstractFactory> @end
抽象工廠子類BJAbstractFactory:
@implementation BJAbstractFactory
-(Cloth *)provideCloth{
return [[Cloth alloc] init];
}
@end
Cloth類:
@implementation Cloth
-(void)clothMethod{
NSLog(@"Cloth--Cloth");
}
@end
Manufactor類中添加兩個新的方法:
-(void)configFactory:(AbstractFactory *)factory{
abstractFactory=factory;
}
-(void)startWithFactory:(NSString *)type{
Dress *myDress=[self createDress:type];
cloth=[abstractFactory provideCloth];
[cloth clothMethod];
[myDress provideMaterial];
[myDress product];
}
方法調用:
Manufactor *factor=[[BJManufactor alloc]init];
[factor configFactory:[[BJAbstractFactory alloc] init]];
[factor startWithFactory:@"BJ"];
NSLog(@"博客園-FlyElephant");
NSLog(@"http://www.cnblogs.com/xiaofeixiang/");
效果:

從實現邏輯來看抽象工廠模式和工廠模式和類似,抽象工廠模式的重點在於創建抽象接口來創建一組相關的產品。抽象工廠模式中,抽象工廠定義接口,所有的具體工廠必須實現該接口,該接口包含一組方法用來生產產品。每個具體工廠能夠生產一整組的產品。工程實踐中根據實際情況進行擇優選擇~
