简体   繁体   中英

c# how to approach Abstract Factory with different signatures (?)

I am trying to figure out how to create different baddies in a game.

let's say I have 2 factions. Paladins and Thieves, and 3 warrior types for each faction

I have an abstractFactory that generates the equipment they are carrying, but I'm not sure how to make it work. Here is what I have in place

enum PaladinWarrior
{
    Squire,
    Novice,
    Knight

}

/// <summary>
/// abstract weapon factory
/// </summary>
abstract class EquipmentFactory
{
    public abstract IWeapon GetWeapon(Enum warriorType);
    /// maybe some other methods like armor and hp
}

/// <summary>
/// concrete weapon factory
/// </summary>
class PaladinWeaponFactory : EquipmentFactory
{
    public override IWeapon GetWeapon(PaladinWarrior warriorType)
    {
        switch (warriorType)
        {
            case PaladinWarrior.Squire:
                return new ShortSword();
            case PaladinWarrior.Novice:
                return new LongSword();
            case PaladinWarrior.Knight:
                return new GreatSword();
            default:
                break;
        }
        
    }
}

/// another factory for the thieves, etc.

The problem i am facing is that by specifying the Enum type, it no longer matches the abstract factory, and would not be straight forward to make it work. in other words, the abstract factory's getWeapon doesnt' have the same signature as the Palading one, and i don't know how to approach that.

I am wondering what pattern/workflow would be better for this? considering I have multiple factions and each faction needs different warriors (With different equipment, of course).

Edit: I wanted the Factory so i could Create a warrior like this:


Warrior goodGuy = new Warrior(PaladinWarrior.Squire)
Warrior badGuy = new Warrior(ThievesWarrior.BBEV) 

and have the Warrior class be able to create the armor and equipment. I don't know what other pattern could accomplish that other than a factory.

Since the question talks about adding warrior types in a game I will answer from a game development perspective

The approach with different enums and different factories creating the warriors is overly complicated, it would be much simpler to merge the enums and have only a single factory that creates the entire Warrior object.

However, in "proper game development" even that is not a good idea, because you want to give the choice of adding and modifying these warrior types to designers, who do not want to (and should not have to) modify code.

Instead you want to make your approach data-driven, which means that all the data necessary to create a Warrior is stored externally in a file or database, using a pre-defined format (which can be a common one like JSON or something completely custom). Then all your code has to do is read in that data and create the objects based on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM