1

ex:

public class Game{ 
  String level;

  public void update(){
    update+"level"(); //calls diff. method depending on variable
  }

  public static void setLevel(String lv){
  level = lv;
  }

  private updateLevelOne(){
  .....
  }

  private updateLevelTwo(){
  .....
  }      
}

public class Level{......
      Game.setLevel(One);
}

I know the top statement wont work. But I was wondering if its possible to make a call in such way that I wouldn't have to use a if/switch statement and go directly to the method.

Hozei
  • 33
  • 3
  • 2
    And are you open to use to Reflection APIs? You know If and Switch statements are there exactly for this kind of conditional branching of code. But if you have any specific aversion to them, well... :) – Praba Apr 30 '14 at 14:49
  • Nothing specific, just still learning and figured it might be faster if i had a lot of levels. – Hozei Apr 30 '14 at 14:52
  • 1
    [This is what you want](http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string/161005#161005) – Steve P. Apr 30 '14 at 14:55

2 Answers2

2

Either use a switch statement or objects:

Switch statement:

public class Game {

    private int level;

    public void update() {
        switch(level) {
            case 1:
                updateLevelOne();
                break;
            case 2:
                updateLevelTwo();
                break;
        }
    }

    public static void setLevel(int lv) {
        level = lv;
    }

    private updateLevelOne() {
        .....
    }

    private updateLevelTwo() {
        .....
    }
}

Alternatively, make your levels objects:

public class Game {

    private Level[] levels;
    private int currentLevel;

    public Game() {
        levels = new Level[2]
        levels[0] = new Level();
        levels[1] = new Level();
        currentLevel = 0;
    }

    public void update() {
        levels[currentLevel].update();
    }

    public static void setLevel(int newLevel) {
        currentLevel = newLevel;
    }
}

public class Level {

    public Level() {

    }

    public void update() {

    }
}

Objects are preferred but you can go either way. You could also go with the reflection package, but that's a worse idea that will be harder to understand.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • OP specifically mentioned no If or Switch. And if the updateLevel implementation is different for different levels and even if we manage to create that many level objects, there will be someplace where we still have to do if level is 1, instantiate level 1 and if level is 2, instantiate level 2 class. Or am I missing something? – Praba Apr 30 '14 at 14:58
  • 1
    Considering what I'm trying to do objects might work better. I used them to create my enemy's but it didn't click that I could do the same thing with my levels. Thanks. Prabugp - most of my levels have that same fields so i think it should work – Hozei Apr 30 '14 at 15:06
  • @user3461480 Generally using objects is better. Only update the level you're on. You can also create and destroy levels dynamically, store different lists of enemies in your levels, and so on. – Anubian Noob Apr 30 '14 at 15:08
1

No, it's not possible to create method names dynamically like this.

You should look up the Strategy pattern. This means you set some Level interface as a field in your class. All your different levels should implement this interface, and each should implement some update method. At runtime, you can set this field with the exact implementation that you need at that time and you can call update() on it. This will delegate to the required implementation.

user1717259
  • 2,717
  • 6
  • 30
  • 44
  • I agree that this pattern is good from a design stand point. But " At runtime, you can set this field with the exact implementation that you need at that time " - to do this we will need a conditional branching based on level correct? – Praba Apr 30 '14 at 15:00