提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类;具体的工厂负责实现具体的产品实例
抽象工厂中每个工厂可以创建多种产品(如苹果公司生产iPhone、iPad);
工厂方法每个工厂只能创建一种
假设有骷髅士兵、骷髅弓箭手、哥布林士兵、哥布林弓箭手四种怪物
做一个简单得怪物生成器,在不死族地带生成不死族怪物,在哥布林地带生成哥布林怪物;怪物分别有士兵和弓箭手两种
创建抽象产品族类、抽象产品类——怪物、怪物士兵、怪物弓箭手
// 抽象产品族类 —— 怪物
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<UUndeadSkeletonSoldier>(); // 实际使用 SpawnActor
}
// 生成骷髅弓箭手
virtual UMonsterArcher* CreateMonsterArcher() override {
return NewObject<UUndeadSkeletonArcher>();
}
};
// 具体工厂类 —— 哥布林怪物生成器
UCLASS(Blueprintable, BlueprintType)
class DESIGNPATTERNS_API UGoblinMonsterFactory : public UMonsterFactory
{
GENERATED_BODY()
public:
// 生成哥布林士兵
virtual UMonsterSoldier* CreateMonsterSoldier() override {
return NewObject<UGoblinSoldier>(); // 实际使用 SpawnActor
}
// 生成哥布林弓箭手
virtual UMonsterArcher* CreateMonsterArcher() override {
return NewObject<UGoblinArcher>();
}
};
测试调用
// 调用测试用的Actor
UCLASS()
class DESIGNPATTERNS_API AMonsterGenerator : public AActor
{
GENERATED_BODY()
public:
void BeginPlay() override {
// 不死族地带区域 生成怪物
UUndeadMonsterFactory* UndeadMonsterFactory = NewObject<UUndeadMonsterFactory>();
UndeadMonsterFactory->CreateMonsterSoldier()->ShowInfo();
UndeadMonsterFactory->CreateMonsterArcher()->ShowInfo();
// 哥布林地带生成 怪物
UGoblinMonsterFactory* GoblinMonsterFactory = NewObject<UGoblinMonsterFactory>();
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
【UE4 C++】抽象工厂模式 Abstract Factory Pattern
原文:https://www.cnblogs.com/shiroe/p/14852327.html