Skip to content

Instantly share code, notes, and snippets.

@tylertreat
Created December 31, 2014 21:24
Show Gist options
  • Select an option

  • Save tylertreat/a24eb6090198db2af7eb to your computer and use it in GitHub Desktop.

Select an option

Save tylertreat/a24eb6090198db2af7eb to your computer and use it in GitHub Desktop.
public abstract class LazilyLoadedObject implements InvocationHandler {
private Object target;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (target == null)
target = loadObject();
return method.invoke(target, args);
}
/**
* Loads the proxied object. This might be an expensive operation
* or loading lots of objects could consume a lot of memory, so
* we only load the object when it's needed.
*/
protected abstract Object loadObject();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment