目录
描述
三种工厂小结
套路
使用场景
优缺点
UE4实践
抽象产品族(怪物)
抽象产品(怪物士兵、怪物弓箭手)
具体产品类(骷髅士兵、骷髅弓箭手、哥布林士兵、哥布林弓箭手)
抽象工厂类-怪物生成器
具体工厂类-不死族怪物生成器
具体工厂类-哥布林怪物生成器
假设有骷髅士兵、骷髅弓箭手、哥布林士兵、哥布林弓箭手四种怪物
一个简单的怪物生成器,不死族地带生成不死组怪物,哥布林地带生成哥布林怪物;
怪物分别由士兵和弓箭手
// 抽象产品族类 —— 怪物
UCLASS(Abstract)
class DESIGNPATTERNS_API UMonster : public UObject
{
GENERATED_BODY()
public:
virtual void ShowInfo() { check(0 && "You must override this"); }
};
// 抽象产品类、具体产品族 —— 怪物士兵
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UMonsterSoldier : public UMonster
{
GENERATED_BODY()
public:
virtual void ShowInfo() override {
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" This is a Monster Soldier"));
}
};
// 抽象产品类、具体产品族 —— 怪物弓箭手
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UMonsterArcher : public UMonster
{
GENERATED_BODY()
public:
virtual void ShowInfo() override {
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" This is a Monster Archer"));
}
};
// 具体产品类 —— 怪物士兵 —— 骷髅士兵
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UUndeadSkeletonSoldier : public UMonsterSoldier
{
GENERATED_BODY()
public:
virtual void ShowInfo() override {
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" This is a Undead Skeleton Soldier"));
}
};
// 具体产品类 —— 怪物士兵 —— 哥布林士兵
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UGoblinSoldier : public UMonsterSoldier
{
GENERATED_BODY()
public:
virtual void ShowInfo() override {
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" This is a Goblin Soldier"));
}
};
// 具体产品类 —— 怪物弓箭手 —— 骷髅弓箭手
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UUndeadSkeletonArcher : public UMonsterArcher
{
GENERATED_BODY()
public:
virtual void ShowInfo() override {
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" This is a Undead Skeleton Archer"));
}
};
// 具体产品类 —— 怪物弓箭手 —— 哥布林弓箭手
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UGoblinArcher : public UMonsterArcher
{
GENERATED_BODY()
public:
virtual void ShowInfo() override {
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" This is a Goblin Archer"));
}
};
// 抽象工厂类 —— 怪物生成器
UCLASS(Abstract)
class DESIGNPATTERNS_API UMonsterFactory : public UObject
{
GENERATED_BODY()
public:
// 生成士兵
virtual UMonsterSoldier* CreateMonsterSoldier() {
check(0 && "You must override this");
return nullptr;
}
// 生成弓箭手
virtual UMonsterArcher* CreateMonsterArcher(){
check(0 && "You must override this");
return nullptr;
}
};
// 具体工厂类 —— 不死族怪物生成器
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UUndeadMonsterFactory : public UMonsterFactory
{
GENERATED_BODY()
public:
// 生成骷髅士兵
virtual UMonsterSoldier* CreateMonsterSoldier() override {
return NewObject(); // 实际使用 SpawnActor
}
// 生成骷髅弓箭手
virtual UMonsterArcher* CreateMonsterArcher() override {
return NewObject();
}
};
// 具体工厂类 —— 哥布林怪物生成器
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UGoblinMonsterFactory : public UMonsterFactory
{
GENERATED_BODY()
public:
// 生成哥布林士兵
virtual UMonsterSoldier* CreateMonsterSoldier() override {
return NewObject(); // 实际使用 SpawnActor
}
// 生成哥布林弓箭手
virtual UMonsterArcher* CreateMonsterArcher() override {
return NewObject();
}
};
测试
// 调用测试用的Actor
UCLASS()
class DESIGNPATTERNS_API AMonsterGenerator : public AActor
{
GENERATED_BODY()
public:
void BeginPlay() override {
// 不死族地带区域 生成怪物
UUndeadMonsterFactory* UndeadMonsterFactory = NewObject();
UndeadMonsterFactory->CreateMonsterSoldier()->ShowInfo();
UndeadMonsterFactory->CreateMonsterArcher()->ShowInfo();
// 哥布林地带生成 怪物
UGoblinMonsterFactory* GoblinMonsterFactory = NewObject();
GoblinMonsterFactory->CreateMonsterSoldier()->ShowInfo();
GoblinMonsterFactory->CreateMonsterArcher()->ShowInfo();
}
};
LogTemp: Warning: UUndeadSkeletonSoldier::ShowInfo This is a Undead Skeleton Soldier
LogTemp: Warning: UUndeadSkeletonArcher::ShowInfo This is a Undead Skeleton Archer
LogTemp: Warning: UGoblinSoldier::ShowInfo This is a Goblin Soldier
LogTemp: Warning: UGoblinArcher::ShowInfo This is a Goblin Archer