何為建造者模式?
1.當(dāng)我們需要?jiǎng)?chuàng)建一個(gè)復(fù)雜的對象,那么我們可以使用多個(gè)簡單的對象來構(gòu)建。
2.主要解決在軟件系統(tǒng)中,有時(shí)候面臨著"一個(gè)復(fù)雜對象"的創(chuàng)建工作,其通常由各個(gè)部分的子對象用一定的算法構(gòu)成;由于需求的變化,這個(gè)復(fù)雜對象的各個(gè)部分經(jīng)常面臨著劇烈的變化,但是將它們組合在一起的算法卻相對穩(wěn)定
3.建造者獨(dú)立,容易拓展,便于控制細(xì)節(jié)風(fēng)險(xiǎn)。但產(chǎn)品必須有共同點(diǎn),如果內(nèi)部負(fù)責(zé),會有很多建造類。
通過下面的例子,可以更好的理解這個(gè)問題。
一輛汽車需要有什么組成?
Engine,Wheels,Door
我們需要為這個(gè)三個(gè)對象創(chuàng)建三個(gè)協(xié)議,這三個(gè)協(xié)議,類似于這個(gè)對象的說明書,表示這幾個(gè)對象的尺寸拉,等基本信息。
@protocol AbstractEngineProtocol <NSObject>
@required
/**
引擎的尺寸
@param scale 尺寸
*/
- (void)engineScale:(CGFloat)scale;
/**
引擎的重量
@param kg 重量
*/
- (void)engineWeight:(CGFloat)kg;
/**
信息
@return 引擎信息
*/
- (NSString *)infomation;
@end
@protocol AbstractWheelsProtocol <NSObject>
@required
/**
輪子的數(shù)目
@param number 數(shù)目
*/
- (void)wheelsNumber:(NSNumber *)number;
/**
信息
@return 引擎信息
*/
- (NSString *)infomation;
@end
@protocol AbstractDoorProtocol <NSObject>
@required
/**
門的顏色
@param color 顏色
*/
- (void)doorColor:(UIColor *)color;
/**
信息
@return 引擎信息
*/
- (NSString *)infomation;
@end
我們的目的是在有Engine,Wheels,Door這個(gè)三個(gè)對象的前提下,直接可以組裝出最后的車子。
所以我們還需要實(shí)現(xiàn)一個(gè)build協(xié)議和build對象。
build協(xié)議會由Engine,Wheels,Door這個(gè)三個(gè)對象繼承。
@protocol BuilderPotocol <NSObject>
@required
/**
構(gòu)建對象
@return 返回構(gòu)建的對象
*/
- (id)build;
@end
build對象中有包含了Engine,Wheels,Door這個(gè)三個(gè)對象,同時(shí)需要讓他們遵循build協(xié)議和自己信息的協(xié)議
@interface Builder : NSObject
@property (nonatomic, strong) id <BuilderPotocol, AbstractEngineProtocol> engine;
@property (nonatomic, strong) id <BuilderPotocol, AbstractWheelsProtocol> wheels;
@property (nonatomic, strong) id <BuilderPotocol, AbstractDoorProtocol> door;
//產(chǎn)品信息
@property (nonatomic, strong) NSDictionary *productsInfo;
/**
構(gòu)建所有部件
*/
- (void)buildAllParts;
@end
@implementation Builder
- (void)buildAllParts {
// 創(chuàng)建所有部件
[self.engine build];
[self.wheels build];
[self.door build];
NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
// 組裝產(chǎn)品
dataDic[@"engine"] = [self.engine infomation];
dataDic[@"wheels"] = [self.wheels infomation];
dataDic[@"door"] = [self.door infomation];
self.productsInfo = dataDic;
}
@end
最后我們創(chuàng)建engine wheels door三個(gè)對象,并讓他們分別遵守并實(shí)現(xiàn)BuilderPotocol和記錄自己說明書的協(xié)議。
@interface Engine : NSObject <BuilderPotocol, AbstractEngineProtocol>
@end
@implementation Engine
- (void)engineScale:(CGFloat)scale {
// todo
}
- (void)engineWeight:(CGFloat)kg {
// todo
}
- (NSString *)infomation {
return @"X1-Engine, scale : 10, weight : 100";
}
- (id)build {
// todo
return nil;
}
@end
@interface Wheels : NSObject <BuilderPotocol, AbstractWheelsProtocol>
@end
@implementation Wheels
- (void)wheelsNumber:(NSNumber *)number {
// todo
}
- (NSString *)infomation {
return @"X1-wheels, number : 4";
}
- (id)build {
// todo
return nil;
}
@end
@interface Door : NSObject <BuilderPotocol, AbstractDoorProtocol>
@end
@implementation Door
- (void)doorColor:(UIColor *)color {
// todo
}
- (NSString *)infomation {
return @"X1-door, color : red";
}
- (id)build {
// todo
return nil;
}
@end
如何來使用這個(gè)建造者呢?
@interface ViewController ()
@property (nonatomic, strong) Builder *builder;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建組裝者
self.builder = [[Builder alloc] init];
// 指定承包商
self.builder.engine = [[Engine alloc] init];
self.builder.wheels = [[Wheels alloc] init];
self.builder.door = [[Door alloc] init];
// 構(gòu)建所有的部件
[self.builder buildAllParts];
// 獲取產(chǎn)品
NSLog(@"%@", self.builder.productsInfo);
}
@end
就這樣,我們通過協(xié)議,實(shí)現(xiàn)了通過engine wheels door組裝除了汽車。