Reputation: 23972
Task: I want to use some methods for many classes. Methods are same, so there is no need to implement them for each class. In my case - I work with android SDK and I send http request to server.
Problem: There is idea to use construction like this:
class abstract MethodsCarrier{
public static void method1(){ /*something*/ }
public static int method2(){ /*return something*/ }
}
It works, there is no problems. But I'm not sure about making this class abstract. Is it's a right way at all?
Upvotes: 4
Views: 424
Reputation: 108830
I'd prefer a final
class with no public constructors. That's the way sun did it with the Math
class, so I assume that's the conventional way to do this in java.
But in your case, I'd avoid static. Static methods are good for side effect free functions that don't access any external(or global mutable) state. A http request does access external state.
I'd define an interface with this methods, write one implementation that does the requests, and then use an IoC container to inject it into the consuming code in a singleton context. That way you can mock the interface, so you don't need to make http requests while testing.
Upvotes: 6