Objective-C 生成器模式 -- 简单实用和说明
1.生成器模式的定义
将一个复杂的对象的构件与它的表示分离,使得同样的构建过程可以创建不同的表示
2.生成器模式的UML
Builder :生成器接口,定义创建一个Product各个部件的操作
ConcreteBuilder:具体的生成器的实现类
Product:产品,表示被生成器构建的复杂的对象,包含多个部件
Director:指导者也称导向者,主要用来使用Builder接口 ,已一个统一的接口创建Product对象
比如我们要生产一辆汽车, 简单分为生产发动机, 轮子, 车门, 分别由发动机部门, 轮子部门, 车门部门分别负责生产
由生产调度中心调度, 并将3个部门的产品合成为一辆汽车
首先我们创建一个生产调度中心Build类负责调度其他生产部门, 这个时候需要给生产部门一个约束或者是协议, 比如部门要有统一的生产方法
然后我们以接口的形式创建各个部门的抽象接口
AbstractEngineProtocol.h
1 #import <Foundation/Foundation.h> 2 #import <UIKit/UIkit.h> 3 4 @protocol AbstractEngineProtocol <NSObject> 5 6 @required 7 8 /** 9 设置发动机尺寸 10 11 @param scale 尺寸大小 12 */ 13 - (void)engineScale:(CGFloat)scale; 14 15 /** 16 设置发动机重量 17 18 @param height 发动机重量 19 */ 20 - (void)engineWeight:(CGFloat)height; 21 22 /** 23 获取发动机信息 24 25 @return 返回发动机信息 26 */ 27 - (NSString *)information; 28 29 @end
AbstractWheelProtocol.h
1 #import <Foundation/Foundation.h> 2 #import <UIKit/UIKit.h> 3 4 @protocol AbstractWheelProtocol <NSObject> 5 6 @required 7 8 /** 9 设置轮子个数 10 11 @param number 个数 12 */ 13 - (void)wheelNumber:(CGFloat)number; 14 15 /** 16 返回轮子信息 17 18 @return 轮子信息 19 */ 20 - (NSString *)information; 21 22 @end
AbstractDoorProtocol.h
1 #import <Foundation/Foundation.h> 2 #import <UIKit/UIKit.h> 3 4 @protocol AbstractDoorProtocol <NSObject> 5 6 @required 7 8 /** 9 设置门的数量 10 11 @param number 数量 12 */ 13 - (void)doorNumber:(CGFloat)number; 14 15 /** 16 返回车门的信息 17 18 @return 车门信息 19 */ 20 - (NSString *)information; 21 22 @end
BuildProtocal.h
1 #import <Foundation/Foundation.h> 2 3 @protocol BuildProtocal <NSObject> 4 5 @required 6 7 /** 8 生产部件 9 10 @return 返回部件 11 */ 12 - (id)build; 13 14 @end
Builder.h
1 #import <Foundation/Foundation.h> 2 #import "AbstractDoorProtocol.h" 3 #import "AbstractWheelProtocol.h" 4 #import "AbstractEngineProtocol.h" 5 #import "BuildProtocal.h" 6 7 @interface Builder : NSObject 8 9 @property (nonatomic, strong) id <BuildProtocal, AbstractEngineProtocol> engine; 10 @property (nonatomic, strong) id <BuildProtocal, AbstractWheelProtocol> wheel; 11 @property (nonatomic, strong) id <BuildProtocal, AbstractDoorProtocol> door; 12 13 /** 14 产品信息 15 */ 16 @property (nonatomic, strong) NSDictionary *productInfo; 17 18 /** 19 构建所有部件 20 21 @return 返回产品 22 */ 23 - (id)buildAllParts; 24 25 @end
Builder.m
1 #import "Builder.h" 2 3 @implementation Builder 4 5 - (id)buildAllParts { 6 7 //创建部件 8 [self.engine build]; 9 [self.wheel build]; 10 [self.door build]; 11 12 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 13 14 //组装产品 15 dict[@"engine"] = [self.engine information]; 16 dict[@"wheel"] = [self.wheel information]; 17 dict[@"door"] = [self.door information]; 18 19 self.productInfo = dict; 20 21 return self; 22 } 23 24 @end
接下来我们创建各个部门的类, 他们实现部门抽象接口的同时还要遵循调度中心的约束, 实现调度中心的接口
BMWEngine.h
1 #import <Foundation/Foundation.h> 2 #import "AbstractEngineProtocol.h" 3 #import "BuildProtocal.h" 4 5 @interface BMWEngine : NSObject <AbstractEngineProtocol, BuildProtocal> 6 7 8 @end
BMWEngine.m
#import "BMWEngine.h" @implementation BMWEngine #pragma mark - AbstractEngineProtocol methods - (void)engineScale:(CGFloat)scale { //to do } - (void)engineWeight:(CGFloat)height { //to do } - (NSString *)information { return @"BMW Engine, Scale: 10, Height: 100kg"; } #pragma mark - BuildProtocol method - (id)build { return [BMWEngine new]; } @end
MIQIWheel.h
1 #import <Foundation/Foundation.h> 2 #import "BuildProtocal.h" 3 #import "AbstractWheelProtocol.h" 4 5 @interface MIQIWheel : NSObject <BuildProtocal, AbstractWheelProtocol> 6 7 @end
MIQIWheel.m
1 #import "MIQIWheel.h" 2 3 @implementation MIQIWheel 4 5 #pragma mark - AbstractWheelProtocol methods 6 7 - (void)wheelNumber:(CGFloat)number { 8 9 //to do 10 } 11 12 - (NSString *)information { 13 14 return @"MIQI Wheel, number: 4"; 15 } 16 17 #pragma mark - BuildProtocol method 18 19 20 - (id)build { 21 22 return [MIQIWheel new]; 23 } 24 25 @end
BMWDoor.h
1 #import <Foundation/Foundation.h> 2 #import "BuildProtocal.h" 3 #import "AbstractDoorProtocol.h" 4 5 @interface BMWDoor : NSObject <BuildProtocal, AbstractDoorProtocol> 6 7 @end
BMWDoor.m
1 #import "BMWDoor.h" 2 3 @implementation BMWDoor 4 5 #pragma mark - AbstractDoorProtocol methods 6 7 - (void)doorNumber:(CGFloat)number { 8 9 //to do 10 } 11 12 - (NSString *)information { 13 14 return @"BMW Door, number: 4"; 15 } 16 17 #pragma mark - BuildProtocol method 18 19 - (id)build { 20 21 return [BMWDoor new]; 22 } 23 24 @end
下面是在Controller中实现
1 #import "ViewController.h" 2 #import "Builder.h" 3 #import "BMWEngine.h" 4 #import "BMWDoor.h" 5 #import "MIQIWheel.h" 6 7 @interface ViewController () 8 9 @end 10 11 @implementation ViewController 12 13 - (void)viewDidLoad { 14 15 [super viewDidLoad]; 16 17 //创建一个建造调控中心 18 Builder *builder = [[Builder alloc] init]; 19 20 //配置生产组件单位 21 builder.engine = [[BMWEngine alloc] init]; 22 builder.wheel = [[MIQIWheel alloc] init]; 23 builder.door = [[BMWDoor alloc] init]; 24 25 //生产 26 id product = [builder buildAllParts]; 27 28 //打印产品信息 29 NSLog(@"%@", [product productInfo]); 30 } 31 32 33 @end