mardi 6 novembre 2018

Factory pattern with class type as parameter

I have several Bot objects:

Slicer
Bomber
Tower
Zombie
Fly
Slime
Soldier
...

I want to build a bot factory. Is passing a class type to the constructor an anti-pattern?

public class BotFactory {

        public static Bot create(Game game, Class<? extends Bot> botClass) {
            Bot bot = null;
            if (botClass == Soldier.class)
                    bot = new Soldier(game, 0, 0);
            else if (botClass == Slicer.class)
                    bot = new Slicer(game, 0, 0);
            // ...

            return bot;
        }
    }

Aucun commentaire:

Enregistrer un commentaire