Methods return objects of different types… here is a solution to the problem.
Methods return objects of different types
I’m using gson and Volley to make HTTP GET requests. My idea is to have a method return an object that contains the serialized JSON.
public responseHolder getRequest(){
Make call
Parse Json into JsonObject
return responseHolder;
}
My problem is that I want the method to handle different API calls that return different data. I have 3 classes designed to store 3 different calls and I want the method to return an object of the correct type. Is there a method or design pattern that can help me in this situation, or should I approach it from a different angle?
Solution
Maybe try this?
private String getRequest()
{
Make Call
return jsonString;
}
public TypeA getA()
{
return new Gson().fromJson(getRequest(), TypeA.class);
}
public TypeB getB()
{
return new Gson().fromJson(getRequest(), TypeB.class);
}
public TypeC getC()
{
return new Gson().fromJson(getRequest(), TypeC.class);
}