何為生成器模式夹供?
生成器模式:將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表現(xiàn)分離抚官,使得同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表現(xiàn)猜旬。
生成器模式包含兩個(gè)重要的角色:Director和Builder。
Builder知道究竟如何在缺少某些特定信息的情況下建造產(chǎn)品甩卓。Director則知道Builde應(yīng)該建造什么鸠匀,以參數(shù)向其提供缺少的信息來(lái)建造特定的產(chǎn)品。
靜態(tài)關(guān)系如圖所示:
Builder是一個(gè)抽象接口逾柿,聲明了一個(gè)BuildPart方法缀棍,該方法由ConcreteBuilder實(shí)現(xiàn),以構(gòu)造實(shí)際產(chǎn)品机错。ConcreteBuilder有個(gè)GetResult方法爬范,向客戶端返回構(gòu)造完畢的Product。
Direct定義了一個(gè)Construct方法弱匪,命令Builder的實(shí)例去BuildPart青瀑。Director和Builder形成了一種聚合的關(guān)系。
這意味著B(niǎo)uilder是一個(gè)組成部分萧诫,與Director結(jié)合斥难,以使整個(gè)模式運(yùn)轉(zhuǎn),但同時(shí)帘饶,Director并不管理Builder的生存期哑诊。
何時(shí)使用?
1.需要?jiǎng)?chuàng)建涉及各種部件的復(fù)雜對(duì)象及刻。創(chuàng)建對(duì)象的算法應(yīng)該獨(dú)立于部件的裝配方式镀裤。
2.構(gòu)建過(guò)程需要以不同的方式構(gòu)建對(duì)象。
例子:
我們將以假想的追逐游戲?yàn)槔煞梗菔救绾斡袃煞N類型的角色--敵人和游戲者暑劝。
兩種角色有一些共同的基本特征,如力量茴扁、耐力铃岔、智力汪疮、敏捷和攻擊力峭火。每一特征都影響角色的防御與攻擊能力毁习。防御因子反映了角色防御攻擊了的能力,而攻擊因子反映了攻擊對(duì)手的能力卖丸。
創(chuàng)建一個(gè)Character對(duì)象:
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只是定義了角色的共有特征,并不知道如何把自己構(gòu)建成有意義的角色稍浆,所以才需要CharacterBuilder給予先前定義的特征關(guān)系载碌,構(gòu)建有意義的角色。
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的實(shí)例有個(gè)對(duì)目標(biāo)Character的引用衅枫,該目標(biāo)Character構(gòu)建完成后將被返回給客戶端嫁艇。
StandardCharacterBuilder是CharacterBuilder的子類,定義了生成具有各種相關(guān)特征的真正角色的邏輯弦撩。
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沒(méi)有重載buildNewCharacter方法益楼,是因?yàn)榛惖囊呀?jīng)夠用了猾漫。
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
接下來(lái)是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的實(shí)例。然后向ChasingGame發(fā)送createPlayer:和createEnemy:消息感凤。
以上就是生成器模式的具體應(yīng)用悯周。