如何消除switch

使用多态来消除switch:Link.

With polymorphism, this:

foreach (var animal in zoo) {
    switch (typeof(animal)) {
        case "dog":
            echo animal.bark();
            break;

        case "cat":
            echo animal.meow();
            break;
    }
}

becomes this:

foreach (var animal in zoo) {
    echo animal.speak();
}
原文地址:https://www.cnblogs.com/evanxyhu/p/ways-to-eliminate-switch-in-code.html