Return to Global Object Global objects with references to all application services
1: package com.voipplus.mmsclient.serviceManager;
2:
3: import android.app.Application;
4: import android.util.Log;
5:
6: import com.voipplus.mmsclient.httpService.HTTPService;
7: import com.voipplus.mmsclient.serviceLocalStorage.CryptoStorageService;
8: import com.voipplus.mmsclient.serviceLocalStorage.KeyValueService;
9: import com.voipplus.mmsclient.serviceMMS.MMSProcessingService;
10: import com.voipplus.mmsclient.WebRTC.WebRTCService;
11:
12: public class MyApplication extends Application {
13: private static final String TAG = "MyApplication";
14: public final ServiceLocatorReference serviceLocator = new ServiceLocatorReference();
15:
16: @Override
17: public void onCreate() {
18: super.onCreate();
19: Log.d(TAG, "onCreate: MyApplication started");
20: // Register service classes here
21: registerServiceClasses();
22: }
23:
24: private void registerServiceClasses() {
25: // Register service classes, not instances
26: serviceLocator.registerService(KeyValueService.class, null);
27: serviceLocator.registerService(CryptoStorageService.class, null);
28: serviceLocator.registerService(MMSProcessingService.class, null);
29: serviceLocator.registerService(HTTPService.class, null);
30: serviceLocator.registerService(WebRTCService.class, null);
31: }
32:
33: public <T> void registerServiceInstance(Class<T> serviceClass, T serviceInstance) {
34: serviceLocator.registerService(serviceClass, serviceInstance);
35: }
36: }
37:
1: package com.voipplus.mmsclient.serviceManager;
2:
3: public interface ServiceLocatorInterface {
4: <T> T getService(Class<T> serviceClass);
5: }
6:
1: package com.voipplus.mmsclient.serviceManager;
2:
3: import java.util.HashMap;
4: import java.util.Map;
5:
6: public class ServiceLocatorReference implements ServiceLocatorInterface {
7: private final Map<Class<?>, Object> services = new HashMap<>();
8:
9: @Override
10: public <T> T getService(Class<T> serviceClass) {
11: Object service = services.get(serviceClass);
12: if (serviceClass.isInstance(service)) {
13: return serviceClass.cast(service);
14: } else {
15: throw new IllegalArgumentException("Service " + serviceClass.getName() + " not found");
16: }
17: }
18:
19: public <T> void registerService(Class<T> serviceClass, T service) {
20: services.put(serviceClass, service);
21: }
22: }
23:
Return to Global Object
AndroidMosaic context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/AndroidMosaic/ServiceLocator.htm
|
|