Java 关于实施连接池的问题

Java 关于实施连接池的问题,java,design-patterns,Java,Design Patterns,我在一次采访中被问到以下问题。 给我的密码是 interface Connection { void execute(); void close(); } interface ConnectionPool { Connection getConnection(); } class Client { ConnectionPool connectionPool = new MyConnectionPool() ; Connection connect

我在一次采访中被问到以下问题。 给我的密码是

interface Connection {
    void execute();
    void close();
}


interface ConnectionPool {
    Connection getConnection();
}

class Client {

    ConnectionPool connectionPool = new MyConnectionPool() ;

    Connection connection  = connectionPool.getConnection();

    public void execute() {
        try{
        connection.execute();
    }finally {
            connection.close();
        }
    }
}

class MyConnectionPool implements ConnectionPool {

    MyConnectionPool(List<Connection> connections) {

    }
    @Override
    public Connection getConnection() {
        return null;
    }
}
接口连接{
void execute();
无效关闭();
}
接口连接池{
连接getConnection();
}
类客户端{
ConnectionPool ConnectionPool=新的MyConnectionPool();
Connection=connectionPool.getConnection();
public void execute(){
试一试{
connection.execute();
}最后{
connection.close();
}
}
}
类MyConnectionPool实现ConnectionPool{
MyConnectionPool(列出连接){
}
@凌驾
公共连接getConnection(){
返回null;
}
}
这里连接的实现是由其他人完成的,我只能更改MyConnectionPool类或创建新类。问题是,每当客户端调用connection.close时,连接对象实际上是关闭的,要求它应该被添加回其他人可以使用的连接池

我的实现就像

class MyConnectionPool implements ConnectionPool {

    Queue<Connection> connections;

    MyConnectionPool(List<Connection> connections) {
        this.connections = new LinkedList(connections);
    }
    @Override
    public Connection getConnection() {
        MyConnection connection = new MyConnection(connections.poll(), this);
        return connection;
    }

    public void closeConnection(Connection connection) {
        connections.add(connection);
    }
}

class MyConnection implements Connection {


    Connection connection;
    boolean isClosed = false;
    MyConnectionPool connectionPool;

    MyConnection(Connection connection, MyConnectionPool connectionPool) {
        this.connection = connection;
    }

    @Override
    public void execute() {
        if (!isClosed){
            connection.execute();
        }
    }

    @Override
    public void close() {
        if (!isClosed) {
            connectionPool.closeConnection(this);
        }

    }
}
类MyConnectionPool实现ConnectionPool{
队列连接;
MyConnectionPool(列出连接){
this.connections=新的LinkedList(连接);
}
@凌驾
公共连接getConnection(){
MyConnection connection=新的MyConnection(connections.poll(),this);
回路连接;
}
公共连接(连接连接){
连接。添加(连接);
}
}
类MyConnection实现连接{
连接;
布尔值isClosed=false;
MyConnectionPool连接池;
MyConnection(连接连接,MyConnectionPool连接池){
这个连接=连接;
}
@凌驾
public void execute(){
如果(!isClosed){
connection.execute();
}
}
@凌驾
公众假期结束(){
如果(!isClosed){
connectionPool.closeConnection(此);
}
}
}

对吗?有没有更好的方法来实现同样的效果。

你的界面
ConnectionPool
是否应该有类似
releaseConnection(Connection)
的方法,以便客户端可以使用它将连接返回到池中?@AlexRudenko这是他们给出的示例问题,它没有这种方法。而且客户端代码是不能更改的。听起来你在找一个。所以有了这样的限制,你做得很对。可能您只是忘记了在
MyConnection
的构造函数中存储
connectionPool
,并在
close
:)中设置isClosed=true。在
MyConnection
MyConnectionPool
之间存在循环依赖关系,这显然是不正确的。您的接口
connectionPool
不应该有方法吗比如
releaseConnection(Connection)
,这样客户端就可以使用它将连接返回到池中?@AlexRudenko这是他们给出的示例问题,它没有这种方法。而且客户端代码是不能更改的。听起来你在找一个。所以有了这样的限制,你做得很对。可能您只是忘记了在
MyConnection
的构造函数中存储
connectionPool
,并在
close
:)中设置isClosed=true。在
MyConnection
MyConnectionPool
之间存在循环依赖关系,这显然是不正确的。