Skip to content

Instantly share code, notes, and snippets.

@amitkma
Created August 20, 2019 15:05
Show Gist options
  • Save amitkma/1c03994127cac64680b29a0e1c58e0c2 to your computer and use it in GitHub Desktop.
Save amitkma/1c03994127cac64680b29a0e1c58e0c2 to your computer and use it in GitHub Desktop.
class ServiceLocator...
private static ServiceLocator sInstance;
public static void load(ServiceLocator arg) {
sInstance = arg;
}
private Map services = new HashMap();
public static Object getService(String key){
return sInstance.services.get(key);
}
public void loadService (String key, Object service) {
services.put(key, service);
}
.....
.....
}
// A simple assembler class.
class Tester {
private void configure() {
ServiceLocator locator = new ServiceLocator();
locator.loadService("LocalBookFinder", new LocalBookFinder());
locator.loadService("RemoteBookFinder", new RemoteBookFinder());
ServiceLocator.load(locator);
}
}
class Library...
BookFinder finder = (BookFinder) ServiceLocator.getService("LocalBookFinder");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment