Skip to content

Instantly share code, notes, and snippets.

@mjedynak
Created August 6, 2016 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjedynak/eb92c40df625ae52950c0dadb4b56268 to your computer and use it in GitHub Desktop.
Save mjedynak/eb92c40df625ae52950c0dadb4b56268 to your computer and use it in GitHub Desktop.
public class LazyInitWithProxyServiceFactory {
public static Service createService() {
return Reflection.newProxy(Service.class, new LazyInitInvocationHandler());
}
private static class LazyInitInvocationHandler extends AbstractInvocationHandler {
private Service service;
@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(service(), args);
}
private Service service() {
if (service == null) {
service = new DefaultService();
}
return service;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment