23种设计模式-The PoolManager using Proxy


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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值