Skip to content

Instantly share code, notes, and snippets.

@ipolevoy
Created January 2, 2014 21:07
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 ipolevoy/8226928 to your computer and use it in GitHub Desktop.
Save ipolevoy/8226928 to your computer and use it in GitHub Desktop.
Suggestion for design top open ActiveJDBC connections in desktop applications
package test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
public class ConnectionDemo {
public static void main(String[] args) {
PeopleFacadeImpl peopleFacade = new PeopleFacadeImpl();
PeopleFacade proxy = (PeopleFacade) Proxy.newProxyInstance(PeopleFacade.class.getClassLoader(),new Class[]{PeopleFacade.class}, new DatabaseConnectionHandler(peopleFacade));
System.out.println(proxy.getPeople());
}
}
interface PeopleFacade{
public List<String> getPeople();
}
class PeopleFacadeImpl implements PeopleFacade{
@Override
public List<String> getPeople() {
List<String> people = new ArrayList<>();
people.add("Bob");
people.add("Jane");
return people;
}
}
class DatabaseConnectionHandler implements InvocationHandler {
private final PeopleFacade proxied;
public DatabaseConnectionHandler(PeopleFacade proxied) {
this.proxied = proxied;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Here you would open connection");
Object returnValue = method.invoke(proxied, args);
System.out.println("Here you would close connection");
return returnValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment