jeudi 30 septembre 2021

How do I get rid of a ever growing switch-statement?

The Problem

switch (choice) {
    case 0:
        return new Class0();
    case 1:
        return new Class1();
    case 2:
        return new Class2();
    case <n - 1>:
        return new Class<n - 1>();
    default
        return new Class<n>();
}

What I Tried

Chain of responsibility design pattern. A great and amazing solution for distributing complex tasks. However, I don't want a class that checks if it can do 1 method.


Motivation

I'm tired of updating the code every time I add something. Bad design.

Aucun commentaire:

Enregistrer un commentaire