Abstract class with static methods. It's right?

Challenge . I want to use some methods for many classes. The methods are the same, so there is no need to implement them for each class. In my case - I work with the android SDK and send an HTTP request to the server.

Problem . There is an idea to use such a construction:

class abstract MethodsCarrier{ public static void method1(){ /*something*/ } public static int method2(){ /*return something*/ } } 

It works, no problem. But I'm not sure about making this class abstract. Is it correct?

+4
source share
1 answer

I would prefer the final class without public constructors. The way the sun did this with the Math class, so I guess the usual way to do this is in java.


But in your case, I would have avoided static. Static methods are good for free side-effect functions that don't have access to an external (or globally mutable) state. An HTTP request has access to an external state.

I would define an interface with these methods, write one implementation that executes queries, and then use the IoC container to enter it into the consumption code in a singleton context. This way you can mock the interface, so you do not need to make HTTP requests during testing.

+6
source

Source: https://habr.com/ru/post/1388798/


All Articles