package design; import java.util.ArrayList; /** * Created by Administrator on 2018/4/18. */ public class PoolManager { private static class PoolItem{ boolean inUse=false; Object item; PoolItem(Object item){ this.item=item; } } //添加代理 public class ReleasableReference{ private PoolItem reference; private Boolean released=false; public ReleasableReference(PoolItem reference){ this.reference=reference; } public Object getReference(){ if(released) throw new RuntimeException( "Tried to use reference after it was released"); return reference.item; } public void release() { released = true; reference.inUse = false; } } private ArrayList items=new ArrayList(); public void add(Object item){ items.add(new PoolItem(item)); } public static class EmptyPoolItem{} public ReleasableReference get() { for(int i = 0; i < items.size(); i++) { PoolItem pitem = (PoolItem)items.get(i); if(pitem.inUse == false) { pitem.inUse = true; return new ReleasableReference(pitem); } } return null; } static class EmptyPoolException extends Exception{} /* public Object get() throws EmptyPoolException{ for(int i=0;i<items.size();i++){ PoolItem poolItem=(PoolItem) items.get(i); if(poolItem.inUse==false) poolItem.inUse=true; return poolItem.item; } throw new EmptyPoolException(); }*/ public void release(Object item){ for(int i=0;i<items.size();i++){ PoolItem poolItem=(PoolItem) items.get(i); if(item==poolItem.item){ poolItem.inUse=false; return; } } throw new RuntimeException(item+"not found"); }
}
package design; /** * Created by Administrator on 2018/4/18. */ public interface Connection { Object get(); void set(Object x); void release(); }
package design; /** * Created by Administrator on 2018/4/18. */ public class ConnectionImplementation implements Connection { public Object get() { return null; } public void set(Object x) { } public void release() {} }
package design; import proxy.*; import proxy.ConnectionImplementation; /** * Created by Administrator on 2018/4/18. */ public class ConnectionPool { private static PoolManager poolManager=new PoolManager(); public static void addConnection(int number){ for(int i=0;i<number;i++) poolManager.add(new ConnectionImplementation()); } public static Connection getConnection() throws PoolManager.EmptyPoolException { /*return (Connection)poolManager.get();*/ PoolManager.ReleasableReference rr = (PoolManager.ReleasableReference)poolManager.get(); if(rr == null) return null; return new ConnectionProxy(rr); } public static void releaseConnection(Connection c) { poolManager.release(c); } private static class ConnectionProxy implements Connection { private PoolManager.ReleasableReference implementation; public ConnectionProxy(PoolManager.ReleasableReference rr) { implementation = rr; } public Object get() { return ((Connection)implementation.getReference()).get(); } public void set(Object x) { ((Connection)implementation.getReference()).set(x); } public void release() { implementation.release(); } } }
package design; /** * Created by Administrator on 2018/4/19. */ public class ConnectionPoolProxyDemo { static { ConnectionPool.addConnection(5); } public void test() throws Exception{ Connection c = ConnectionPool.getConnection(); c.set(new Object()); c.get(); c.release(); } public void testDisable() throws Exception{ Connection c = ConnectionPool.getConnection(); String s = null; c.set(new Object()); c.get(); c.release(); try { c.get(); } catch(Exception e) { s = e.getMessage(); System.out.println(s); } } public static void main(String args[]) { junit.textui.TestRunner.run( ConnectionPoolProxyDemo.class); } }