By HYBR1D


2018-02-13 13:02:30 8 Comments

I'm trying to make a small game with a TUI. The TUI should have a get() method that should take a String question and a return type as parameter, so you can use it to request an object.

For example, get("What is your name?", String) should return a String and get("What color do you want?", Color) should return a Color.

This is what I've come up with:

public interface View {

    public <T> T get(String question, Class<T> type) throws InputException;

}

public class TUI implements View {

    public Color get(String question, Class<Color> type) {
        // code
    }

    public String get(String question, Class<String> type) {
        // code
    }

}

Color color = Game.getView().get("What piece do you want to place?", Color.class);

Unfortunately, the methods in TUI are not accepted as implementations of public <T> T get(String question, Class<T> type) throws InputException;. Is there any way I can do this?

4 comments

@Pado 2018-02-13 13:56:57

You can use something similar to Template Method Design Pattern.

public interface TypeTemplate {
    public Object get(String question);
}
public class ColorTemplate implements TypeTemplate {
    @Override public Color get(String question) { /*code for color*/ }
}
public class StringTemplate implements TypeTemplate {
    @Override public String get() { /*code for string*/ }
}

public class TUI {
    public Color get(String question, ColorTemplate templ) {
        return templ.get(String question)
    }
    public String get(String question, StringTemplate templ) {
        return templ.get(String question);
    }
}



public static void main(String args[]) {
    TUI tui = new TUI();
    String string = tui.get("What is your name?", new StringTemplate());
    Color color = tui.get("What color do you want?", new ColorTemplate());
}

You can of course replace Object type in TypeTemplate with something less generic. You can also make TypeTemplate one abstract class; this way if all (or some) of your get() methods will have to share some code you can add methods in this class and call from the subclasses. e.g.

public abstract class TypeTemplate {
    public abstract Object get(String question);
    protected void init() {/*some initialization code*/} 
    protected void cleanup(){/*some more code*/}
}
public class ColorTemplate implements TypeTemplate {
    @Override
    public Color get(String question) {
        init();
        /*code for color*/
        cleanup(); 
    }
}
public class StringTemplate implements TypeTemplate {
    @Override
    public String get() {
        init();
        /*code for string*/
        cleanup(); 
    }
}

@David 2018-02-13 13:32:49

Your TUI class is not valid because under type erasure both get methods have the same arguments.

You might want to do this:

public interface View<T> {
    T get(String question, Class<T> type) throws InputException;
}

public class ColorTUI implements View<Color> {
    public Color get(String question, Class<Color> type) {
        // code
    }
}

public class StringTUI implements View<String> {
    public String get(String question, Class<String> type) {
        // code
    }
}

@HYBR1D 2018-02-13 13:50:01

Adding multiple classes will only make things more complicated. I'm trying to make my code more simple. Isn't there any way to write multiple get methods in TUI using different return types, without adding all methods to the View interface?

@David 2018-02-13 15:52:58

No, that is not possible in Java. This would not even work without the interface (i.e. if you have only the TUI class), because the methods would have the same call signature due to type erasure.

@Alberto 2018-02-13 13:31:42

Try to make two classes AskColor and AskString that implements View, with method respectively

  public Color get(String question, Class<Color> type) {
    // code
}

public String get(String question, Class<String> type) {
    // code
}

and the interface View with method

public T get(String question, Class<T> type) throws InputException;

@Alan Blyth 2018-02-13 13:08:28

Keeping it type safe by splitting the functions would seem more appropriate:

Color color = Game.getView().getColor("What piece do you want to place?");
String string = Game.getView().getString("What piece do you want to place?");

Related Questions

Sponsored Content

57 Answered Questions

[SOLVED] How to read / convert an InputStream into a String in Java?

61 Answered Questions

[SOLVED] How to generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3559958 View
  • 2981 Score
  • 61 Answer
  • Tags:   java random integer

27 Answered Questions

[SOLVED] How to determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1590474 View
  • 1926 Score
  • 27 Answer
  • Tags:   java arrays

18 Answered Questions

[SOLVED] How do I call one constructor from another in Java?

  • 2008-11-12 20:10:19
  • ashokgelal
  • 690423 View
  • 1892 Score
  • 18 Answer
  • Tags:   java constructor

77 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

38 Answered Questions

[SOLVED] How to efficiently iterate over each entry in a Java Map?

35 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

19 Answered Questions

[SOLVED] Does Java support default parameter values?

29 Answered Questions

[SOLVED] How to round a number to n decimal places in Java

24 Answered Questions

[SOLVED] Why can't I define a static method in a Java interface?

Sponsored Content