设计模式第二次测试 | 数据库连接池设计(原型模式、创建者模式、适配器模式)

发布于:2024-05-01 ⋅ 阅读:(31) ⋅ 点赞:(0)

需求中文如下:原本是英文,用百度翻译转换而来

我们需要设计一个工具,它负责创建一个与数据库软件MySQL的连接池。

连接池中有数百个连接可供客户端使用。

所有连接对象都有相同的内容,但它们是不同的对象

连接对象的创建是资源密集型的,因此建立与数据库软件MySQL的连接需要一些时间。

初始化连接池时,需要尽快创建数百个连接对象。

该工具提供以下功能:

使用指定的字符串初始化数据库信息。

创建连接池对象时,初始化具有指定连接数的连接池。

客户端可以从该池中获取连接以使用。

客户端可以在完成数据库操作后将连接放回该池

MySQL数据库软件提供了创建连接的驱动程序:

驱动程序提供了一种静态方法来初始化带有数据库信息的连接,包括协议、IP地址、端口号、数据库名称、用户名和密码。

一开始,驱动程序只支持一个协议“jdbc”,并且驱动程序的设计不支持未来的新协议。

但是驱动程序现在需要支持名为“xdbc”的新协议,而我们不能通过继承来做到这一点。

为了利用现有的jdbc实现,我们必须提供将xdbc转换为jdbc的能力。

连接的接口:

使用数据库信息初始化连接,数据库信息是指定的字符串。

客户端可以使用sql字符串将记录添加到表中。

客户端可以使用sql字符串从表中删除记录。

客户端可以使用sql字符串更新表中的记录。

客户端可以使用sql字符串从表中搜索记录。

首先我们要对需求进行分析,由创建数百个连接,相同内容、不同对象可以很容易看出这是要使用原型模式了,其实这里说是建造者模式还是十分牵强的,所以实际上只用了原型模式。具体设计如下:

代码如下:

import java.util.Scanner;

class Connection implements Cloneable {
    private String Protocol;
    private String IPAddress;
    private String Port;
    private String SQLName;
    private String username;
    private String password;
    public Connection clone() {
        Connection r = null;
        try {
            r = (Connection)super.clone();
        }  catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        return r;
    }
    public void init(String Protocol, String IPAddress, String Port, String SQLName, String username, String password) {
        this.Protocol = Protocol;
        this.IPAddress = IPAddress;
        this.Port = Port;
        this.SQLName = SQLName;
        this.username = username;
        this.password = password;
    }

    public Connection getConnection(String Protocol, String IPAddress, String Port, String SQLName, String username, String password) {
        Connection mycon = new Connection();
        mycon.init(Protocol, IPAddress, Port, SQLName, username, password);
        return (Connection)mycon.clone();
    }
    public void dropoutConnection(Connection c) {
        System.out.println("drop out connection");
    }
    public void show() {
        System.out.println(Protocol + " " + IPAddress + " " + Port + " "
                + SQLName + " "  + username + " " + password);
    }

}

class Main1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        Connection mycon = new Connection();
        mycon.init("jdbc", "127.0.0.1","8080","fistSQL", "root", "123456");
        while (N-- > 0) {
            Connection r = (Connection) mycon.clone();
            r.show();
        }
    }

}

再由一开始只支持一个jdbc协议,现在还需要支持xdbc协议,但是驱动程序本身无法再支持新的协议,看出需要使用到适配器模式

设计图如下:

设计代码如下:

import java.util.*;

interface Protocol {
    String ProtocolContent = null;
    void show();
}

class jdbc implements Protocol {
    String ProtocolContent = "jdbc";
    public jdbc init() {
        return new jdbc();
    }
    public void show() {
        System.out.println(ProtocolContent);
    }
}

class xdbc {
    String ProtocolContent = "xdbc";
    public xdbc init() {
        return new xdbc();
    }
    public void show() {
        System.out.println(ProtocolContent);
    }
}

class Adapter extends jdbc {
    String ProtocolContent = null;
    public Adapter Adapte(xdbc x) {
        this.ProtocolContent = x.ProtocolContent;
        return this;
    }
}


class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        if (s.equals("xdbc")) {
            xdbc myxdbc = new xdbc();
            Adapter myAdapter = new Adapter();
            myAdapter.Adapte(myxdbc);
            jdbc myjdbc = myAdapter;
            myjdbc.show();
        }
    }

}

这次考试感觉能写的都调出来了,但是感觉可能还要涉及到数据库连接池本身的涉及,我在设计的时候把连接都只用一个类来当对象了,没涉及到继承,这估计会是一个扣分点了。等着明天挨批了。


网站公告

今日签到

点亮在社区的每一天
去签到