1.生成器模式的定义
2.生成器模式的UML
比如我们要生产一辆汽车, 简单分为生产发动机, 轮子, 车门, 分别由发动机部门, 轮子部门, 车门部门分别负责生产
由生产调度中心调度, 并将3个部门的产品合成为一辆汽车
首先我们创建一个生产调度中心Build类负责调度其他生产部门, 这个时候需要给生产部门一个约束或者是协议, 比如部门要有统一的生产方法
然后我们以接口的形式创建各个部门的抽象接口
AbstractEngineProtocol.h
#import <Foundation/Foundation.h>
#import <UIKit/UIkit.h> @protocol AbstractEngineProtocol <NSObject> @required /**
设置发动机尺寸 @param scale 尺寸大小
*/
- (void)engineScale:(CGFloat)scale; /**
设置发动机重量 @param height 发动机重量
*/
- (void)engineWeight:(CGFloat)height; /**
获取发动机信息 @return 返回发动机信息
*/
- (NSString *)information; @end
AbstractWheelProtocol.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @protocol AbstractWheelProtocol <NSObject> @required /**
设置轮子个数 @param number 个数
*/
- (void)wheelNumber:(CGFloat)number; /**
返回轮子信息 @return 轮子信息
*/
- (NSString *)information; @end
AbstractDoorProtocol.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @protocol AbstractDoorProtocol <NSObject> @required /**
设置门的数量 @param number 数量
*/
- (void)doorNumber:(CGFloat)number; /**
返回车门的信息 @return 车门信息
*/
- (NSString *)information; @end
BuildProtocal.h
#import <Foundation/Foundation.h> @protocol BuildProtocal <NSObject> @required /**
生产部件 @return 返回部件
*/
- (id)build; @end
Builder.h
#import <Foundation/Foundation.h>
#import "AbstractDoorProtocol.h"
#import "AbstractWheelProtocol.h"
#import "AbstractEngineProtocol.h"
#import "BuildProtocal.h" @interface Builder : NSObject @property (nonatomic, strong) id <BuildProtocal, AbstractEngineProtocol> engine;
@property (nonatomic, strong) id <BuildProtocal, AbstractWheelProtocol> wheel;
@property (nonatomic, strong) id <BuildProtocal, AbstractDoorProtocol> door; /**
产品信息
*/
@property (nonatomic, strong) NSDictionary *productInfo; /**
构建所有部件 @return 返回产品
*/
- (id)buildAllParts; @end
Builder.m
#import "Builder.h" @implementation Builder - (id)buildAllParts { //创建部件
[self.engine build];
[self.wheel build];
[self.door build]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; //组装产品
dict[@"engine"] = [self.engine information];
dict[@"wheel"] = [self.wheel information];
dict[@"door"] = [self.door information]; self.productInfo = dict; return self;
} @end
接下来我们创建各个部门的类, 他们实现部门抽象接口的同时还要遵循调度中心的约束, 实现调度中心的接口
BMWEngine.h
#import <Foundation/Foundation.h>
#import "AbstractEngineProtocol.h"
#import "BuildProtocal.h" @interface BMWEngine : NSObject <AbstractEngineProtocol, BuildProtocal> @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
#import <Foundation/Foundation.h>
#import "BuildProtocal.h"
#import "AbstractWheelProtocol.h" @interface MIQIWheel : NSObject <BuildProtocal, AbstractWheelProtocol> @end
MIQIWheel.m
#import "MIQIWheel.h" @implementation MIQIWheel #pragma mark - AbstractWheelProtocol methods - (void)wheelNumber:(CGFloat)number { //to do
} - (NSString *)information { return @"MIQI Wheel, number: 4";
} #pragma mark - BuildProtocol method - (id)build { return [MIQIWheel new];
} @end
BMWDoor.h
#import <Foundation/Foundation.h>
#import "BuildProtocal.h"
#import "AbstractDoorProtocol.h" @interface BMWDoor : NSObject <BuildProtocal, AbstractDoorProtocol> @end
BMWDoor.m
#import "BMWDoor.h" @implementation BMWDoor #pragma mark - AbstractDoorProtocol methods - (void)doorNumber:(CGFloat)number { //to do
} - (NSString *)information { return @"BMW Door, number: 4";
} #pragma mark - BuildProtocol method - (id)build { return [BMWDoor new];
} @end
下面是在Controller中实现
#import "ViewController.h"
#import "Builder.h"
#import "BMWEngine.h"
#import "BMWDoor.h"
#import "MIQIWheel.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建一个建造调控中心
Builder *builder = [[Builder alloc] init]; //配置生产组件单位
builder.engine = [[BMWEngine alloc] init];
builder.wheel = [[MIQIWheel alloc] init];
builder.door = [[BMWDoor alloc] init]; //生产
id product = [builder buildAllParts]; //打印产品信息
NSLog(@"%@", [product productInfo]);
} @end