从一个ConnectionPool的实现看design pattern的运用 (二) (转)

从一个ConnectionPool的实现看design pattern的运用 (二) (转)[@more@] 

从一个ConnectionPool的实现看design pattern的运用 ()XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

好啦,现在让我们看看我们是怎样设计这个ConnectionPool的接口的。

Public interface ConnectionPool{

  Connection getConnection();

  Void clear();

}

当然,这只是一个简化的接口。真正实现时也许我们还会有一些其他的方法。如同时支持同步getConnection()和异步getConnection().

同时,这个返回的Connection必须改写close()方法。原来的close()方法是关闭物理连接,但现在,我们要把这个Connection返还到ConnectionPool. 因此,这个Connection对象必须知道它出身的ConnectionPool.

这样,用户仍然调用Connection.close(), 也不用担心把Connection返还到错误的ConnectionPool.

 

再来看看我们的实现:

public class ConnectionPoolImpl: implements ConnectionPool{

  class PooledConnection implements Connection{

    private final Connection conn;

    private Boolean closed;

    public PooledConnection(Connection conn)

throws sqlException{

    this.conn = conn;

    closed = conn.isClosed();

  }

    public void close(){

    if(!closed){

//保证重复调用close()不会把一个connection重复返还。

      closeConnection(conn);

      closed = true;

    }

  }

    public Boolean isClosed(){return closed;}

    public Statement createStatement()

  throws SQLConnection{

    if(isClosed()) throw new SQLException(“Connection closed”);

    return conn.createStatement();

  }

    public void commit()

  throws SQLConnection{

    if(isClosed()) throw new SQLException(“Connection closed”);

    conn.commit();

  }

    //其他所有的方法都这样做委托。

  }

public synchronized Connection getConnection(){

    如果pool里有Connection

从pool中去掉一个Connection conn;

clients++;

return new PooledConnection(conn);

    否则,如果clients

    生成一个新的连接conn

    clients++;

    return new PooledConnection(conn);

    否则,wait(),直到pool中有空闲Connection   

  }

  //其他的实现都和我们第一章里的代码一样。

}

 

好了,这样,通过内部类PooledConnection这样一个wrapper, 我们就可以实现这个能处理ConnectionPool的Connection.

对了,忘了说了,这个PooledConnection其实就是design pattern里的decorator模式

 

现在让我们再欣赏一下我们的代码吧。ConnectonPoolImpl提供了一种基于简单策略的ConnectionPool的实现。PooledConnection封装了数据库连接,使之能和ConnectionPool协同工作。

完美吧?

 

 

 

不过,慢着!PooledConnection只是和ConnectionPoolImpl协同工作。张三要写ConnectionPool2还得自己重新实现一个decorator.

怎么样能使不同的ConnectionPool的实现重用我们PooledConnection呢?

 

而且,在所有大约二十个委托函数里,我们都有if(isClosed())……, 是不是很眼熟啊?一个Connection一旦被close()之后,只有在涅磐之后(通过ConnectionPool.getConnection()再次返回),才能使用。而所有对关闭了的Connection的调用,返回结果都是一样的.(不行,不行,不行!)

猜到点什么了吗?对啦,state pattern!

 

 

 


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-992453/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-992453/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值