patternjavaMinor
Static methods or singleton?
Viewed 0 times
methodssingletonstatic
Problem
I have class that handles HTTP requests:
For the one hand - there is no need in instance of class, so I can use static methods. But for another hand singleton looks more reusable. So, I'm not sure which way to choose.
What is more readable and reusable? Static or singleton? Or should I just don't care about it and allow user to create instance of class?
public final class RestHttpClient {
/*there is no fields*/
/**
* @param mhttp - HTTP request that need to send
* @return HttpResponse, that contains information about server response
*/
public HttpResponse sendRequest(final HttpRequestBase mhttp) throws IOException {
/*do stuff and get response from server*/
return response;
}
/*there a few more helpful methods*/
}For the one hand - there is no need in instance of class, so I can use static methods. But for another hand singleton looks more reusable. So, I'm not sure which way to choose.
What is more readable and reusable? Static or singleton? Or should I just don't care about it and allow user to create instance of class?
Solution
This:
Or should I just don't care about it and allow user to create instance
of class?
If you plan to write unit tests, which is recommended these days: for your class AND the code that will use it.
http://googletesting.blogspot.co.uk/2008/05/tott-using-dependancy-injection-to.html
Or should I just don't care about it and allow user to create instance
of class?
If you plan to write unit tests, which is recommended these days: for your class AND the code that will use it.
http://googletesting.blogspot.co.uk/2008/05/tott-using-dependancy-injection-to.html
Context
StackExchange Code Review Q#10484, answer score: 7
Revisions (0)
No revisions yet.