Im writing an android application that contains several (4 to be exact) Services.
Each service has a static boolean field labeled ‘running’. It is set to true when the service is created, and set to false when it is destroyed.
Each service also has a static method labeled ‘isRunning()’ that simply returns ‘running’
Currently, I have identical code in all 4 services to provide this functionality. Ideally, the code should be located in one place. How can this be achieved? An interface? an abstract class? neither?
(I think the fact that this field and method are static place limitations on the answer)
If the methods must be static and available on the involved classes, then they must belong to the individual classes; no way around it. But perhaps you could have a static map of
Class -> booleanwhich kept track of the running services in some central location; when a service is started, it sets the appropriate value in that map by calling a (single) static method for that purpose. Then when anything needs to know ifServiceClassis running, they query that map usingServiceClass.classas the key. Sound good?