建造者模式,又称生成器模式。是将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式将客户端与包含多个组成部分的复杂对象的创建过程分离,客户端无需知道复杂对象的内部组成与装配方式,主需要知道所需的建造者即可。类似工厂方法,但是建造者模式返回一个完整的复杂产品,而抽象工厂模式则返回一系列相关的产品。以汽车为例,工厂方法可以看成不同汽车配件的生成,而建造者模式则可以看成时汽车的组装
做一个简单得门派生成器
创建具体赋值产品 —— 门派角色角色
// 复杂产品 —— 门派角色 UCLASS(Blueprintable, BlueprintType) class DESIGNPATTERNS_API USectPlayer : public UObject { GENERATED_BODY() public: // 门派 FString Sect = TEXT("None"); // 性别 FString Render = TEXT("None"); // 服装 FString Costume = TEXT("None"); // 武器 FString Weapon = TEXT("None"); // 展示信息 void ShowInfo() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" %s %s %s %s"), *Sect, *Render, *Costume, *Weapon); } };
创建抽象建造者 —— 角色构造器,具体建造者 —— 纯阳角色构造器、七秀角色构造器
// 抽象建造者 —— 角色构造器 UCLASS(Abstract) class DESIGNPATTERNS_API USectPlayerBuilder : public UObject { GENERATED_BODY() public: // 构造函数创建复杂角色实例 USectPlayerBuilder() { SectPlayer = NewObject<USectPlayer>(); } // 各个构建部分 virtual void BuilderSect(){} virtual void BuilderRender(){} virtual void BuilderCostume(){} virtual void BuilderWeapon(){} // 返回角色实例 USectPlayer* GetPlayer() { return SectPlayer; } protected: USectPlayer* SectPlayer; }; // 具体建造者 —— 纯阳角色构造器 UCLASS(Blueprintable, BlueprintType) class DESIGNPATTERNS_API UChunYangSectPlayerBuilder : public USectPlayerBuilder { GENERATED_BODY() public: virtual void BuilderSect() override { SectPlayer->Sect = TEXT("纯阳门派"); } virtual void BuilderRender() override { SectPlayer->Render = TEXT("男"); } virtual void BuilderCostume() override { SectPlayer->Costume = TEXT("纯阳服装"); } virtual void BuilderWeapon() override { SectPlayer->Weapon = TEXT("纯阳武器"); } }; // 具体建造者 —— 七秀角色构造器 UCLASS(Blueprintable, BlueprintType) class DESIGNPATTERNS_API UQiXiuSectPlayerBuilder : public USectPlayerBuilder { GENERATED_BODY() public: virtual void BuilderSect() override { SectPlayer->Sect = TEXT("七秀门派"); } virtual void BuilderRender() override { SectPlayer->Render = TEXT("女"); } virtual void BuilderCostume() override { SectPlayer->Costume = TEXT("七秀服装"); } virtual void BuilderWeapon() override { SectPlayer->Weapon = TEXT("七秀武器"); } };
创建指挥者(Director) —— 角色创建指挥者
// 指挥者(Director) —— 角色创建指挥者 UCLASS(Blueprintable, BlueprintType) class DESIGNPATTERNS_API UPlayerCreateDirectory : public USectPlayerBuilder { GENERATED_BODY() public: USectPlayer* Construct(USectPlayerBuilder* SectPlayerBuilder) { // 逐步构建复杂角色 SectPlayerBuilder->BuilderSect(); SectPlayerBuilder->BuilderRender(); SectPlayerBuilder->BuilderCostume(); SectPlayerBuilder->BuilderWeapon(); return SectPlayerBuilder->GetPlayer(); } };
测试调用
// 调用测试用的Actor UCLASS() class DESIGNPATTERNS_API APlayerBuilder : public AActor { GENERATED_BODY() public: void BeginPlay() override { // 创建指挥者 UPlayerCreateDirectory* PlayerCreateDirectory = NewObject<UPlayerCreateDirectory>(); // 指定纯阳角色构造器生成角色 USectPlayer* ChunYangPlayer = PlayerCreateDirectory->Construct(NewObject<UChunYangSectPlayerBuilder>()); ChunYangPlayer->ShowInfo(); // 指定七秀角色构造器生成角色 USectPlayer* QiXiuPlayer = PlayerCreateDirectory->Construct(NewObject<UQiXiuSectPlayerBuilder>()); QiXiuPlayer->ShowInfo(); } };
调式输出
LogTemp: Warning: APlayerBuilder::BeginPlay 纯阳门派 男 纯阳服装 纯阳武器 LogTemp: Warning: APlayerBuilder::BeginPlay 七秀门派 女 七秀服装 七秀武器