消除代码中开关的方法[英] Ways to eliminate switch in code

本文是小编为大家收集整理的关于消除代码中开关的方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

消除代码中使用开关的方法是什么?

推荐答案

开关固定本身不是对抗的,但是如果您以对象为导向的编码,则应考虑使用使用多态而不是使用开关语句.

有多态性,这是:

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

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

变成以下:

foreach (var animal in zoo) {
    echo animal.speak();
}

其他推荐答案

请参阅开关语句闻到:

通常,类似的开关语句散布在整个程序中.如果您在一个开关中添加或删除子句,则通常也必须找到并修复其他子句.

重新分配 https://rads.stackoverflow.com/amzn/click/com/032121213351" rel =" noreferrer" rel =" nofollow noreferrer">对模式进行重构有解决此问题的方法.

如果您的(伪)代码看起来像:

class RequestHandler {

    public void handleRequest(int action) {
        switch(action) {
            case LOGIN:
                doLogin();
                break;
            case LOGOUT:
                doLogout();
                break;
            case QUERY:
               doQuery();
               break;
        }
    }
}

此代码违反开放式关闭原理随之而来的代码. 为了解决此问题,您可以引入"命令"对象:

interface Command {
    public void execute();
}

class LoginCommand implements Command {
    public void execute() {
        // do what doLogin() used to do
    }
}

class RequestHandler {
    private Map<Integer, Command> commandMap; // injected in, or obtained from a factory
    public void handleRequest(int action) {
        Command command = commandMap.get(action);
        command.execute();
    }
}

希望这会有所帮助.

其他推荐答案

开关是一种模式,无论是使用开关语句实现,如果其他链,查找表,OOP多态性,模式匹配或其他内容.

您是否要消除" Switch语句"或" 开关模式"的使用?只有在使用另一种模式/算法时,可以消除第一个,而在大多数情况下,这是不可能的,或者不是这样做的更好的方法.

如果要从代码中消除 Switch语句,则要问的第一个问题是消除Switch语句并使用其他一些技术是有意义的.不幸的是,这个问题的答案是特定于域的.

记住,编译器可以进行各种优化来切换语句.因此,例如,如果您想有效地进行消息处理,那么开关语句几乎是必经之路.但另一方面,基于开关语句运行业务规则可能不是最好的方法,并且应重新搜索应用程序.

这是Switch语句的一些替代方法:

本文地址:https://www.itbaoku.cn/post/627465.html

问题描述

What are the ways to eliminate the use of switch in code?

推荐答案

Switch-statements are not an antipattern per se, but if you're coding object oriented you should consider if the use of a switch is better solved with polymorphism instead of using a switch statement.

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();
}

其他推荐答案

See the Switch Statements Smell:

Typically, similar switch statements are scattered throughout a program. If you add or remove a clause in one switch, you often have to find and repair the others too.

Both Refactoring and Refactoring to Patterns have approaches to resolve this.

If your (pseudo) code looks like:

class RequestHandler {

    public void handleRequest(int action) {
        switch(action) {
            case LOGIN:
                doLogin();
                break;
            case LOGOUT:
                doLogout();
                break;
            case QUERY:
               doQuery();
               break;
        }
    }
}

This code violates the Open Closed Principle and is fragile to every new type of action code that comes along. To remedy this you could introduce a 'Command' object:

interface Command {
    public void execute();
}

class LoginCommand implements Command {
    public void execute() {
        // do what doLogin() used to do
    }
}

class RequestHandler {
    private Map<Integer, Command> commandMap; // injected in, or obtained from a factory
    public void handleRequest(int action) {
        Command command = commandMap.get(action);
        command.execute();
    }
}

Hope this helps.

其他推荐答案

A switch is a pattern, whether implemented with a switch statement, if else chain, lookup table, oop polymorphism, pattern matching or something else.

Do you want to eliminate the use of the "switch statement" or the "switch pattern"? The first one can be eliminated, the second one, only if another pattern/algorithm can be used, and most of the time that is not possible or it's not a better approach to do so.

If you want to eliminate the switch statement from code, the first question to ask is where does it make sense to eliminate the switch statement and use some other technique. Unfortunately the answer to this question is domain specific.

And remember that compilers can do various optimizations to switch statements. So for example if you want to do message processing efficiently, a switch statement is pretty much the way to go. But on the other hand running business rules based on a switch statement is probably not the best way to go and the application should be rearchitected.

Here are some alternatives to switch statement :