I have an existential doubt. I saw an implementation of ServiceLocator pattern in C++ in some blog (Service Locator). So, I'm trying to extend the same implementation on Java SE (without any other auxiliar framework). See below the code.
My question: Is that a valid implementation for the ServiceLocator pattern in java?. If not is the case, what could be the most simple (example) implementation for this pattern in java?
/**
* Where MyService1 and MyService2 are interfaces and MyService1Impl and MyService2Impl its concrete implementation.
*/
public final class MyServiceLocator {
private static MyService1 service1;
private static MyService2 service2;
private MyServiceLocator() {
// No op
}
public static MyService1 getMyService1() {
if (service1 == null) {
throw new NullMyService1Exception();
}
return service1;
}
public static MyService2 getMyService2() {
if (service2 == null) {
throw new NullMyService2Exception();
}
return service2;
}
public static void initService1() {
// Initialize Service1
...
service1 = new MyService1Impl(...);
}
public static void initService2() {
// Initialize Service2
...
service1 = new MyService2Impl(...);
}
}
Aucun commentaire:
Enregistrer un commentaire