Created
January 2, 2014 21:07
-
-
Save ipolevoy/8226928 to your computer and use it in GitHub Desktop.
Suggestion for design top open ActiveJDBC connections in desktop applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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