比多个if(){do}检查更优雅的代码

时间:2015-12-14 11:01:01

标签: java android optimization

可以通过更优雅的解决方案实现以下行为,结合三个标准。如果读了很多那个开关盒是难闻的气味,如果否则会嵌套。

    @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    Switch sr = (Switch) findViewById(R.id.switch_ros);
    Switch srs = (Switch) findViewById(R.id.switch_ros_stream);
    boolean wifi_state = isConnected(this);
     if (buttonView.getId() == R.id.switch_ros & buttonView.isChecked() & wifi_state) {
        Toast.makeText(this, "ros intent ready", Toast.LENGTH_SHORT).show();
    }
    if (buttonView.getId() == R.id.switch_ros & buttonView.isChecked() & !wifi_state) {
        Toast.makeText(this, "log: wifi bad", Toast.LENGTH_SHORT).show();
        sr.setChecked(false);
    }
    if (buttonView.getId() == R.id.switch_ros & !buttonView.isChecked()) {
        Toast.makeText(this, "ros intent stopped", Toast.LENGTH_SHORT).show();
        srs.setChecked(false); // and stop ros intent, automatically calls onCheckedChange again
    }
    if (buttonView.getId() == R.id.switch_ros_stream & buttonView.isChecked() & sr.isChecked()) {
        Toast.makeText(this, "stream intent ready", Toast.LENGTH_SHORT).show();
    }
    if (buttonView.getId() == R.id.switch_ros_stream & buttonView.isChecked() & !sr.isChecked()) {
        Toast.makeText(this, "log: first switch is off", Toast.LENGTH_SHORT).show();
        srs.setChecked(false);
    }
    if (buttonView.getId() == R.id.switch_ros_stream & !buttonView.isChecked()) {
        Toast.makeText(this, "stream intent stopped", Toast.LENGTH_SHORT).show();
        // and stop stream intent
    }
}

3 个答案:

答案 0 :(得分:1)

我会把它们推到enum。这是为你做的前两个。

enum Check {

    IntentReady {

                @Override
                boolean check(CompoundButton buttonView, boolean wifi_state) {
                    return buttonView.getId() == R.id.switch_ros & buttonView.isChecked() & wifi_state;
                }

                @Override
                void apply(Switch sr, Switch srs) {
                    Toast.makeText(this, "ros intent ready", Toast.LENGTH_SHORT).show();
                }

            },
    WIFIBad {

                @Override
                boolean check(CompoundButton buttonView, boolean wifi_state) {
                    return buttonView.getId() == R.id.switch_ros & buttonView.isChecked() & !wifi_state;
                }

                @Override
                void apply(Switch sr, Switch srs) {
                    Toast.makeText(this, "log: wifi bad", Toast.LENGTH_SHORT).show();
                    sr.setChecked(false);
                }

            };

    abstract boolean check(CompoundButton buttonView, boolean wifi_state);

    abstract void apply(Switch sr, Switch srs);
}

public void onCheckedChangedNew(CompoundButton buttonView, boolean isChecked) {
    Switch sr = (Switch) findViewById(R.id.switch_ros);
    Switch srs = (Switch) findViewById(R.id.switch_ros_stream);
    boolean wifi_state = isConnected(this);
    for ( Check c : Check.values()) {
        if ( c.check(buttonView, wifi_state)) {
            c.apply(sr, srs);
        }
    }
}

这里的主要好处是您可以机器人转换代码。完成后,您将获得更大的灵活性和清晰度。

答案 1 :(得分:0)

我建议这样的事情:

if (buttonView.getId() == R.id.switch_ros)
    if (buttonView.isChecked()) {
        if (wifi_state) {
            Toast.makeText(this, "ros intent ready", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "log: wifi bad", Toast.LENGTH_SHORT).show();
            sr.setChecked(false);
        }
    } else {
        Toast.makeText(this, "ros intent stopped", Toast.LENGTH_SHORT).show();
        srs.setChecked(false); // and stop ros intent, automatically calls onCheckedChange again
    }
} else
if (buttonView.getId() == R.id.switch_ros_stream) {
    if (buttonView.isChecked()) {
        if (sr.isChecked()) {
            Toast.makeText(this, "stream intent ready", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "log: first switch is off", Toast.LENGTH_SHORT).show();
            srs.setChecked(false);
        }        
    } else {
        Toast.makeText(this, "stream intent stopped", Toast.LENGTH_SHORT).show();
        // and stop stream intent
    }
}

这将删除不必要的检查(@Stultuske)并且更具可读性。

答案 2 :(得分:0)

我已经根据我在评论中建议的更改重新编写了一些代码:

    @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    Switch sr = (Switch) findViewById(R.id.switch_ros);
    Switch srs = (Switch) findViewById(R.id.switch_ros_stream);
    boolean wifi_state = isConnected(this);

    String text = "";
    if ( buttonView.getId() == R.id.switch_ros){
    if ( buttonView.isChecked()){
        text = wifi_state ? "ros intent ready":"log: wifi bad";
        if ( !wifi_state){
        sr.setChecked(false);
        }
    }
    else{
        text = "ros intent stopped";
        sr.setChecked(false);
    }

    }
    else if ( buttonView.getId() == R.id.switch_ros_stream){
    if ( buttonView.isChecked()){
        text = sr.isChecked() ? "stream intent ready" : "log: first switch is off";
        if ( !sr.isChecked()){
        srs.setChecked(false);
        }
    }
    else{
        text = "stream intent stopped";
    }
    }
    Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
相关问题