简体   繁体   English

如何重构这种类型的switch-case语句?

[英]How to Refactor this type of switch-case statement?

Based on the minIndex, I am setting the different view visibility. 基于minIndex,我设置了不同的视图可见性。 I think it can be refactored as simple way. 我认为可以将其重构为简单的方法。

switch (minIndex) {
    case 0:
        viewOne.setVisibility(View.VISIBLE);
        break;
    case 1:
        viewTwo.setVisibility(View.VISIBLE);
        break;
    case 2:
        viewThree.setVisibility(View.VISIBLE);
        break;
    case 3:
        viewFour.setVisibility(View.VISIBLE);
        break;
    case 4:
        viewFive.setVisibility(View.VISIBLE);
        break;
    case 5:
        viewSix.setVisibility(View.VISIBLE);
        break;
}

How can I refactor this code as a more readable code? 如何将该代码重构为更具可读性的代码?

If the numbers match up nicely with the actual view, you can use an array. 如果数字与实际视图很好地匹配,则可以使用数组。

View[] views = new View[] {viewOne, viewTwo, viewThree, ...};
...
views[minIndex].setVisibility(View.VISIBLE);

I assume you've anonymized your code for this site. 我认为您已经对该网站的代码进行了匿名处理。 I was going to suggest what @Sotirios Delimanolis answered, but I think you are actually doing both too much and too little. 我本来建议@Sotirios Delimanolis回答了什么,但我认为您实际上做得太多了也太少了。

You've bound the controller too much to the view, and you've bound the views too much to each other. 您已将控制器与视图的绑定过多,并且已将视图彼此的绑定过多。 Why should all of them be in the same switch statement? 为什么它们都应该在同一switch语句中?

How much will you need to change if you add another view? 如果添加其他视图,则需要更改多少?

Instead, you should have each view register a PropertyChangeListener with the object that holds minindex . 相反,您应该让每个视图向持有minindex的对象注册一个PropertyChangeListener That's a bad name, by the way. 顺便说一句,这真是个坏名字。 When the object changes minindex , it should send a PropertyChangeEvent to all the listeners. 当对象更改minindex ,应将PropertyChangeEvent发送给所有侦听器。 Each view's listener should check whether the event wants that view to become visible; 每个视图的侦听器都应检查事件是否希望该视图可见。 if so, the view should awaken itself. 如果是这样,则视图应自动唤醒。

class ViewController {
    private PropertyChangeSupport pcs = new PropertyChangeSupport();

    // delegate methods to add and remove listeners to pcs variable.

    private int viewIndex; // Changed for documentation.  Use String instead?

    public void setViewIndex(final int viewIndex) {
        int oldIndex = this.viewIndex;
        this.viewIndex = viewIndex; 
        pcs.firePropertyChange("viewIndex", oldIndex, this.viewIndex);
    }
}

class ViewOne {
    private ViewController vc;
    private final Integer myIndex = 1;

    // Constructor

    public void init() {
        // Never add this or an inner class to another object in a constructor.
        vc.addPropertyChangeListener("viewIndex",
            new PropertyChangeListener() {
                public propertyChange(final PropertyChangeEvent evt) {
                    if (myIndex.equals(evt.getNewValue()) {
                         setVisibility(View.VISIBLE);
                    }
                }
            });
    }
}

The warning about constructors is that if you expose this or an inner class of this to another object in the constructor, that external object may interact with this before it is fully constructed. 有关构造函数的警告是,如果你暴露this还是一个内部类的this在构造函数中的另一个对象,外部对象可以与交互this之前,它是完全构造。 You can build the PCL in the constructor; 您可以在构造函数中构建PCL。 use another method to add it to the controller. 使用另一种方法将其添加到控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM