一、定义
- 生成器模式:将一个复杂对象的构建和它的表现分离,使得同样的过程可以创建不同的表现。
- 生成器模式包括两个重要的角色:指导者和生成器
- 指导者知道如何在知道缺少某些特定信息的情况下建造产品。
二、使用生成器
- 需要创建涉及各种部件的复杂对象。创建对象的算法应该独立于部件的装配方法。
- 构建过程需要以不同的方式构建对象
三、生成器和抽象工厂的对比
生成器 | 抽象工厂 | |
1 | 构建复杂对象 | 构建复杂或抽象对象 |
2 | 以多个步骤构建对象 | 以单一步骤构建对象 |
3 | 以多种方式构建对象 | 以单一方式构建对象 |
4 | 在构建过程的最后一步返回产品 | 立刻返回产品 |
5 | 专注一个特定产品 | 强调一套产品 |
在生成器模式中,再调用指导者的时候,我们必须知道生成器对象,这样才能构成生成器模式。
当我们调用指导者的时候,如果我们不知道生成器对象以及最终生成了怎样的对象,那么这种模式就变成返回抽象产品的工厂方法模式。
抽象工厂:一般用工厂方法生成不同的产品,然后再将这些产品组合在一起,强调该工厂是生产这一套产品的,创建多系列产品。
生成器:生成器将产品的每个部件分别生产(在生成器中,每个部件都没有特殊性),在指导者中,为每个产品增加特殊性,这样就形成了各不相同的特定产品。
我们的产品为电脑,电脑分为主机,屏幕,键盘,鼠标几个部分。
产品类:
1 #import <Foundation/Foundation.h> 2 3 @interface comPuter : NSObject 4 5 @property (nonatomic, strong) NSString * screen; //屏幕 6 @property (nonatomic, strong) NSString * host; //主机 7 @property (nonatomic, strong) NSString * keyboard; //键盘 8 @property (nonatomic, strong) NSString * mouse; //鼠标 9 10 @end 11 12 13 14 #import "comPuter.h" 15 16 @implementation comPuter 17 18 - (instancetype)init 19 { 20 self = [super init]; 21 if(self){ 22 self.screen = @"屏幕"; 23 self.host = @"主机"; 24 self.keyboard = @"键盘"; 25 self.mouse = @"鼠标"; 26 } 27 28 return self; 29 } 30 31 - (NSString *)description 32 { 33 return [NSString stringWithFormat:@"主机为%@, 屏幕为%@,键盘为%@,鼠标为%@", self.host, self.screen, self.keyboard, self.mouse]; 34 } 35 36 @end
生成器类:
我们在生成器中通过创建主机,屏幕,鼠标,键盘而形成了电脑。
1 #import <Foundation/Foundation.h> 2 #import "comPuter.h" 3 4 @interface creatComputer : NSObject 5 6 @property (nonatomic, strong) comPuter * computer; 7 8 - (creatComputer *)creatNewComputer; 9 - (creatComputer *)creatScreen:(NSString *)screen; 10 - (creatComputer *)creatHost:(NSString *)host; 11 - (creatComputer *)creatKeyboard:(NSString *)keyboary; 12 - (creatComputer *)creatMouse:(NSString *)mouse; 13 14 @end 15 16 17 18 #import "creatComputer.h" 19 20 @implementation creatComputer 21 22 - (creatComputer *)creatNewComputer 23 { 24 self.computer = [[comPuter alloc] init]; 25 return self; 26 27 } 28 29 - (creatComputer *)creatScreen:(NSString *)screen 30 { 31 self.computer.screen = screen; 32 return self; 33 } 34 35 - (creatComputer *)creatHost:(NSString *)host; 36 { 37 self.computer.host = host; 38 return self; 39 } 40 41 - (creatComputer *)creatKeyboard:(NSString *)keyboary 42 { 43 self.computer.keyboard = keyboary; 44 return self; 45 } 46 47 - (creatComputer *)creatMouse:(NSString *)mouse 48 { 49 self.computer.mouse = mouse; 50 return self; 51 } 52 53 @end
指导者类:
指导创建不同品牌的电脑。
1 #import <Foundation/Foundation.h> 2 #import "comPuter.h" 3 #import "creatComputer.h" 4 5 6 @interface directorCreater : NSObject 7 8 - (comPuter *)createAcer:(creatComputer *)creater; 9 - (comPuter *)creatlenovo:(creatComputer *)creater; 10 11 @end 12 13 14 15 16 #import "directorCreater.h" 17 18 @implementation directorCreater 19 20 - (comPuter *)createAcer:(creatComputer *)creater 21 { 22 [creater creatNewComputer]; 23 [creater creatHost:@"AcerHost"]; 24 [creater creatScreen:@"AcerScreen"]; 25 [creater creatKeyboard:@"AcerKeyboard"]; 26 [creater creatMouse:@"AcerMouse"]; 27 28 return [creater computer]; 29 } 30 31 - (comPuter *)creatlenovo:(creatComputer *)creater 32 { 33 [creater creatNewComputer]; 34 [creater creatHost:@"lenovoHost"]; 35 [creater creatScreen:@"lenovoScreen"]; 36 [creater creatKeyboard:@"lenovoKeyboard"]; 37 [creater creatMouse:@"lenovoMouse"]; 38 39 return [creater computer]; 40 } 41 42 @end
调用:
1 creatComputer * creater = [[creatComputer alloc] init]; 2 directorCreater * director = [[directorCreater alloc] init]; 3 comPuter * acre = [director createAcer:creater]; 4 NSLog(@"%@", acre); 5 comPuter * lenovo = [director creatlenovo:creater]; 6 NSLog(@"%@", lenovo);
生成:
我们的生成器只生产一种产品那就是——电脑。
生成器模式能帮助构建涉及部件与表现的各种组合的对象。没有这一模式,知道构建对象所需细节的指导者可能最终会变成一个庞大的“神”类。