Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
Typescript Factory class that avoids using a switch statement when using enum as an input?
abstract class Animal {
    abstract sound(): void;
}

class Cat extends Animal {
    sound(){
        console.log("meow");
    }
}

class Dog extends Animal {
    sound(){
        console.log("bark");
    }
}

enum AnimalType {
  Cat = 1,
  Dog = 2,
}

class AnimalFactory {
  static createAnimal(type: AnimalType): Animal {
    switch (type) {
      case AnimalType.Cat:
        return new Cat();
      case AnimalType.Dog:
        return new Dog();
      default:
        throw new Error("unsupported animal type");
    }
  }
}

const cat = AnimalFactory.createAnimal(AnimalType.Cat);
const dog = AnimalFactory.createAnimal(AnimalType.Dog);
cat.sound();
dog.sound();

Playground Link

I'm trying to get better at the Open–closed principle and I'm trying to figure out how to avoid using switch statements and decouple the factory class with the enum when I add more subclasses of Animal. The user via an API is sending over what animal is to be created via a simple enum. Is it possible to modify the AnimalFactory in a way that I don't need to modify the factory class every time I add another enum value and subclass of Animal?

Am I going at this wrong and I need more than an enum to use as an argument?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can define a constant that contains a mapping of enum values to class types, and use a mapped type to create a type mapping:

abstract class Animal {
    abstract sound(): void;
}

class Cat extends Animal {
    sound() {
        console.log("meow");
    }
}

class Dog extends Animal {
    sound() {
        console.log("bark");
    }
}

enum AnimalType {
  Cat = 1,
  Dog = 2,
}

const animals = {
     [AnimalType.Cat]: Cat,
     [AnimalType.Dog]: Dog
};

type Animals = {
     [A in keyof typeof animals]: InstanceType<typeof animals[A]>
};

class AnimalFactory {
  static createAnimal<A extends keyof Animals>(type: A): Animals[A] {
    return new animals[type]();
  }
}

const cat = AnimalFactory.createAnimal(AnimalType.Cat);
const dog = AnimalFactory.createAnimal(AnimalType.Dog);
cat.sound();
dog.sound();

See it on the playground

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda