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

78 Answered Questions

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

60 Answered Questions

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

  • 2008-12-12 18:20:57
  • user42155
  • 3488980 View
  • 2911 Score
  • 60 Answer
  • Tags:   java random integer

32 Answered Questions

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

18 Answered Questions

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

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

29 Answered Questions

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

29 Answered Questions

[SOLVED] How to avoid Java code in JSP files?

  • 2010-07-05 07:24:06
  • former
  • 250540 View
  • 1526 Score
  • 29 Answer
  • Tags:   java jsp scriptlet

19 Answered Questions

[SOLVED] Does Java support default parameter values?

19 Answered Questions

[SOLVED] How do I declare and initialize an array in Java?

  • 2009-07-29 14:22:27
  • bestattendance
  • 3728269 View
  • 1626 Score
  • 19 Answer
  • Tags:   java arrays declare

31 Answered Questions

[SOLVED] How to split a string in Java

  • 2010-08-14 03:01:53
  • riyana
  • 3092994 View
  • 1282 Score
  • 31 Answer
  • Tags:   java string

24 Answered Questions

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

Sponsored Content