何为生成器模式?
生成器模式:将一个复杂对象的构建与它的表现分离,使得同样的构建过程可以创建不同的表现。
生成器模式包含两个重要的角色:Director和Builder。
Builder知道究竟如何在缺少某些特定信息的情况下建造产品。Director则知道Builde应该建造什么,以参数向其提供缺少的信息来建造特定的产品。
静态关系如图所示:
Builder是一个抽象接口,声明了一个BuildPart方法,该方法由ConcreteBuilder实现,以构造实际产品。ConcreteBuilder有个GetResult方法,向客户端返回构造完毕的Product。
Direct定义了一个Construct方法,命令Builder的实例去BuildPart。Director和Builder形成了一种聚合的关系。
这意味着Builder是一个组成部分,与Director结合,以使整个模式运转,但同时,Director并不管理Builder的生存期。
何时使用?
1.需要创建涉及各种部件的复杂对象。创建对象的算法应该独立于部件的装配方式。
2.构建过程需要以不同的方式构建对象。
例子:
我们将以假想的追逐游戏为例,演示如何有两种类型的角色--敌人和游戏者。
两种角色有一些共同的基本特征,如力量、耐力、智力、敏捷和攻击力。每一特征都影响角色的防御与攻击能力。防御因子反映了角色防御攻击了的能力,而攻击因子反映了攻击对手的能力。
创建一个Character对象:
Charachter.h
@interface Character : NSObject
{
@private
float protection_;
float power_;
float strength_;
float stamina_;
float intelligence_;
float agility_;
float aggressiveness_;
}
@property (nonatomic, assign) float protection;
@property (nonatomic, assign) float power;
@property (nonatomic, assign) float strength;
@property (nonatomic, assign) float stamina;
@property (nonatomic, assign) float intelligence;
@property (nonatomic, assign) float agility;
@property (nonatomic, assign) float aggressiveness;
@end
//Character.m
@implementation Character
@synthesize protection = protection_;
@synthesize power=power_;
@synthesize strength=strength_;
@synthesize stamina=stamina_;
@synthesize intelligence=intelligence_;
@synthesize agility=agility_;
@synthesize aggressiveness=aggressiveness_;
- (instancetype)init{
if (self = [super init]) {
protection_ = 1.0;
power_ = 1.0;
strength_ = 1.0;
stamina_ = 1.0;
intelligence_ = 1.0;
agility_ = 1.0;
aggressiveness_ = 1.0;
}
return self;
}
@end
Character定义所有类型角色共有的一套特征。
Character只是定义了角色的共有特征,并不知道如何把自己构建成有意义的角色,所以才需要CharacterBuilder给予先前定义的特征关系,构建有意义的角色。
CharacterBuilder.h
#import "Character.h"
@interface CharacterBuilder : NSObject
{
@protected
Character * character_;
}
@property (nonatomic, strong, readonly) Character * character;
- (CharacterBuilder *) buildNewCharacter;
- (CharacterBuilder *) buildStrength:(float) value;
- (CharacterBuilder *) buildStamina:(float) value;
- (CharacterBuilder *) buildIntelligence:(float) value;
- (CharacterBuilder *) buildAgility:(float) value;
- (CharacterBuilder *) buildAggress:(float) value;
@end
CharacterBuilder.m
@implementation CharacterBuilder
@synthesize character=character_;
- (CharacterBuilder *)buildNewCharacter{
character_ = [[Character alloc] init];
return self;
}
- (CharacterBuilder *)buildStrength:(float)value{
character_.strength = value;
return self;
}
- (CharacterBuilder *)buildStamina:(float)value{
character_.stamina = value;
return self;
}
- (CharacterBuilder *)buildIntelligence:(float)value{
character_.intelligence = value;
return self;
}
- (CharacterBuilder *)buildAgility:(float)value{
character_.agility = value;
return self;
}
- (CharacterBuilder *)buildAggress:(float)value{
character_.aggressiveness = value;
return self;
}
@end
CharacterBuilder的实例有个对目标Character的引用,该目标Character构建完成后将被返回给客户端。
StandardCharacterBuilder是CharacterBuilder的子类,定义了生成具有各种相关特征的真正角色的逻辑。
StandardCharacterBuilder.h
@interface StandardCharacterBuilder : CharacterBuilder
- (CharacterBuilder *) buildStrength:(float) value;
- (CharacterBuilder *) buildStamina:(float) value;
- (CharacterBuilder *) buildIntelligence:(float) value;
- (CharacterBuilder *) buildAgility:(float) value;
- (CharacterBuilder *) buildAggress:(float) value;
@end
再次声明了重载的方法,是为了比较清晰。StandardCharacterBuild没有重载buildNewCharacter方法,是因为基类的已经够用了。
StandardCharacterBuilder.m
@implementation StandardCharacterBuilder
- (CharacterBuilder *)buildStrength:(float)value{
character_.protection *= value;
character_.power *= value;
return [super buildStrength:value];
}
- (CharacterBuilder *)buildStamina:(float)value{
character_.protection *= value;
character_.power *= value;
return [super buildStamina:value];
}
- (CharacterBuilder *)buildIntelligence:(float)value{
character_.protection *= value;
character_.power /= value;
return [super buildIntelligence:value];
}
- (CharacterBuilder *)buildAgility:(float)value{
character_.protection *= value;
character_.power /= value;
return [super buildAgility:value];
}
- (CharacterBuilder *)buildAggress:(float)value{
character_.protection /= value;
character_.power *= value;
return [super buildAggress:value];
}
@end
接下来是Director,ChasingGame类
ChasingGame.h
#import "StandardCharacterBuilder.h"
@interface ChasingGame : NSObject
- (Character *) createPlayer:(CharacterBuilder *) builder;
- (Character *) createEnemy:(CharacterBuilder *) builder;
@end
ChasingGame.m
@implementation ChasingGame
- (Character *)createPlayer:(CharacterBuilder *)builder{
[builder buildNewCharacter];
[builder buildStrength:50.0];
[builder buildStamina:25.0];
[builder buildIntelligence:75.0];
[builder buildAgility:65.0];
[builder buildAggress:35.0];
return [builder character];
}
- (Character *)createEnemy:(CharacterBuilder *)builder{
[builder buildNewCharacter];
[builder buildStrength:80.0];
[builder buildStamina:65.0];
[builder buildIntelligence:35.0];
[builder buildAgility:25.0];
[builder buildAggress:95.0];
return [builder character];
}
@end
客户端代码
CharacterBuilder * characterBuilder = [[StandardCharacterBuilder alloc] init];
ChasingGame * game = [[ChasingGame alloc] init];
Character * player = [game createEnemy:characterBuilder];
Character * enemy = [game createEnemy:characterBuilder];
客户端生成StandardCharacterBuilder和ChasingGame的实例。然后向ChasingGame发送createPlayer:和createEnemy:消息。
以上就是生成器模式的具体应用。