简体   繁体   English

将 else-if 代码更改为更简洁的代码

[英]Changing else-if code to something cleaner

I have following else-if code and I am really getting annoying with a size of the lines needed for it, so I was thinking if there is some more appropriate way to do it which will be more cleaner and more faster?我遵循了 else-if 代码,我真的对它所需的行的大小感到恼火,所以我在想是否有一些更合适的方法来做到这一点,它会更干净、更快?

if (dialogResult == 0) {
            System.out.println("Yes option to try again!");
            if (level.equals(lp.LEVEL1.getLevelName())) {
                System.out.println(lp.LEVEL1.getLevelName());
                level = lp.LEVEL2.getLevelName(); // set level
                totalBall = lp.LEVEL2.getBallCount();
                numberOfExplodes = lp.LEVEL2.getBallExplosion();

            } else if (level.equals(lp.LEVEL2.getLevelName())) {
                System.out.println(lp.LEVEL2.getLevelName());
                level = lp.LEVEL3.getLevelName(); // set level
                totalBall = lp.LEVEL3.getBallCount();
                numberOfExplodes = lp.LEVEL3.getBallExplosion();

            } else if (level.equals(lp.LEVEL3.getLevelName())) {
                System.out.println(lp.LEVEL3.getLevelName());
                level = lp.LEVEL4.getLevelName(); // set level
                totalBall = lp.LEVEL4.getBallCount();
                numberOfExplodes = lp.LEVEL4.getBallExplosion();

            } else if (level.equals(lp.LEVEL4.getLevelName())) {
                System.out.println(lp.LEVEL4.getLevelName());
                level = lp.LEVEL5.getLevelName(); // set level
                totalBall = lp.LEVEL5.getBallCount();
                numberOfExplodes = lp.LEVEL5.getBallExplosion();

            } else if (level.equals(lp.LEVEL5.getLevelName())) {
                System.out.println(lp.LEVEL5.getLevelName());
                level = lp.LEVEL6.getLevelName(); // set level
                totalBall = lp.LEVEL6.getBallCount();
                numberOfExplodes = lp.LEVEL6.getBallExplosion();

            } else if (level.equals(lp.LEVEL6.getLevelName())) {
                System.out.println(lp.LEVEL6.getLevelName());
                level = lp.LEVEL7.getLevelName(); // set level
                totalBall = lp.LEVEL7.getBallCount();
                numberOfExplodes = lp.LEVEL7.getBallExplosion();

            } else if (level.equals(lp.LEVEL7.getLevelName())) {
                System.out.println(lp.LEVEL7.getLevelName());
                level = lp.LEVEL8.getLevelName(); // set level
                totalBall = lp.LEVEL8.getBallCount();
                numberOfExplodes = lp.LEVEL8.getBallExplosion();

            } else if (level.equals(lp.LEVEL8.getLevelName())) {
                System.out.println(lp.LEVEL8.getLevelName());
                level = lp.LEVEL9.getLevelName(); // set level
                totalBall = lp.LEVEL9.getBallCount();
                numberOfExplodes = lp.LEVEL9.getBallExplosion();

            }
            ...

I was thinking switch could go fine here, but I would like to hear other options as well.我认为 switch 在这里可以正常工作,但我也想听听其他选择。

UPDATE: This is my class: lp is of class Balls which is enum and has 3 params in it's constructor:更新:这是我的课程: lp 属于 Balls 类,它是 enum 并且在它的构造函数中有 3 个参数:

public enum BallCount {

    LEVEL1(5,1,"Level 1"),
    LEVEL2(10,2,"Level 2"),
    LEVEL3(15,4,"Level 3");

Make LEVEL1, LEVEL2, LEVEL3.... or `lp' to implement following method:制作LEVEL1, LEVEL2, LEVEL3....或 `lp' 来实现以下方法:

/**
 * Gets Level enum from name.
 */    
public static Level getLevelFromName(String);

/**
 * Gets next level. 
 * Each level may either keeps reference to the next level or 
 * using enum cardinal number can get the next number.
 */
public Level getNextLevel();

Then your if-then gets reduced to following one/two liner with no conditions.然后你的 if-then 被减少到没有条件地跟随一个/两个班轮。

Level nextLevel = Level.getLevelFromName(level).getNextLevel();
level = lp.LEVEL6.getLevelName(); // set level
totalBall = lp.LEVEL6.getBallCount();
numberOfExplodes = lp.LEVEL6.getBallExplosion();

You can replace the conditional with polymorphism.您可以用多态替换条件。 Basically, it means that you move the implementation in the if blocks into the same function on each of the objects.基本上,这意味着您将 if 块中的实现移动到每个对象上的相同函数中。 Then, just invoke the function, and the right code will run.然后,只需调用该函数,就会运行正确的代码。

You can take a look at https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism for a more thorough explanation.您可以查看https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism以获得更详尽的解释。

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

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