I am trying to find the best OO way to do this, and I would appreciate your help in this.
I think the easiest way is to show you how I did it and try to explain what I want (simplified):
abstract public class MyServiceApi { private static MyServiceApi instance = null; public static <T extends MyServiceApi> T getInstance(Class<T> cls) { if (instance == null) { try { instance = cls.newInstance(); } catch (InstantiationException e) {} catch (IllegalAccessException e) {} } return (T) instance; } private private HashMap<String, String> headers; protected MyServiceApi() {} public HashMap<String, String> getHeaders() { return headers; } public void setHeaders(HashMap<String, String> headers) { this.headers = headers; } protected <T extends IMyServiceApiResponse> T send(String url, IMyServiceApiRequest request, Class<T> to) {
Now that you have an idea, I'm stuck on something.
First of all, I donβt know if I did the right thing (in the Java OO way). I think this is not bad, but I do not have enough experience to be sure.
Secondly, when my project is launched, MyServiceApi will keep the same headers, I will not call another API or other credentials. That's why I thought about Singleton: I set the headers when the application started, and then I just had to execute the request. But I believe that UsersApi and ItemsApi extension MyServiceApi is the best way to do it. They use MyServiceApi , they do not expand their capabilities. Also, I'm sure SingleTon are anti-patterns, bad for tests, etc.
So now I am free, and I do not know what to do. How do you do this?
A possible idea is to remove the MyServiceApi abstract and install Singleton on it, having UsersApi and ItemsApi use MyServiceApi , but not an extension, but how would I manage getBaseUrl?
Thank you very much for your help, I am very grateful!