Java语言对象池(Object pool)设计模式实例

商业数据库通常会限制某一时刻可以使用的连接的个数。下面这个例子就用对象池(Object pool)实现了对这些数据库连接的管理。首先,对象池对象的基本管理是作为一个单独的类来实现。

PoolManager .java

package com.enfo.wd.patterns;

import java.util.ArrayList;

public class PoolManager {
    private static class PoolItem{
        boolean inUse=false;
        Object item;
        PoolItem(Object item){
            this.item=item;
        }
    }
    @SuppressWarnings("rawtypes")
    private ArrayList items=new ArrayList();

    @SuppressWarnings("unchecked")
    void add(Object item){
        items.add(new PoolItem(item));
    }
    @SuppressWarnings("serial")
    static class EmptyPoolException extends Exception{}
    public Object get() throws EmptyPoolException{
        for(int i=0;i<items.size();i++){
            PoolItem pitem=(PoolItem) items.get(i);
            if(pitem.inUse==false){
                pitem.inUse=true;
                return pitem.item;
            }
        }
        throw new EmptyPoolException();
    }
    public void release(Object item){
        for(int i=0;i<items.size();i++){
            PoolItem pitem=(PoolItem) items.get(i);
            if(item==pitem.item){
                pitem.inUse=false;
                return;
            }
        }
        throw new RuntimeException(item+"not found");
    }
}

ConnectionPoolDemo .java

package com.enfo.wd.patterns;

interface Connection{
    Object get();
    void set(Object x);
}

class ConnectionImplementation implements Connection{

    @Override
    public Object get() {
        return null;
    }
    @Override
    public void set(Object x) {
    }
}

class ConnectionPool{
    private static PoolManager pool=new PoolManager();
    public static void addConnection(int number){
        for(int i=0;i<number;i++){
            pool.add(new ConnectionImplementation());
        }
    }
    public static Connection getConnection()
        throws PoolManager.EmptyPoolException{
            return (Connection) pool.get();
    }
    public static void releaseConnection(Connection c){
        pool.release(c);
    }
}

public class ConnectionPoolDemo {
    static{
        ConnectionPool.addConnection(5);
    }
    public void test(){
        Connection c=null;
        try {
            c=ConnectionPool.getConnection();
        } catch (PoolManager.EmptyPoolException e) {
            throw new RuntimeException(e);
        }
        c.set(new Object());
        c.get();
        ConnectionPool.releaseConnection(c);
    }
    public void test2(){
        Connection c=null;
        try {
            c=ConnectionPool.getConnection();
        } catch (PoolManager.EmptyPoolException e) {
            throw new RuntimeException(e);
        }
        c.set(new Object());
        c.get();
        ConnectionPool.releaseConnection(c);
    }
    public static void main(String[] args) {
        ConnectionPoolDemo e=new ConnectionPoolDemo();
        e.test();
        e.test2();
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生活中的思索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值