二、实验内容 某软件公司开发人员要创建一个数据库连接池,将指定个数的(如3个)数据库连接对象存储在连接池中,客户端代码可以从池中随机取一个连接对象来连接数据库。试通过对单例类进行改造,设计一个能够自行指定个数实例对象的数据库连接类并Java代码编程实现(请分别用饿汉和懒汉单例模式实现)。 要求: 1、画出UML图; 2、给出程序代码,程序代码有恰当的注释。 3、文档格式正确,排版清晰。
时间: 2023-05-31 17:06:12 浏览: 143
本题不适用于AI智能作答,请参照以下答案。
1. UML图
饿汉单例模式:
![饿汉单例模式UML图](https://img-blog.csdn.net/20180312104727792)
懒汉单例模式:
![懒汉单例模式UML图](https://img-blog.csdn.net/20180312104739382)
2. 代码实现
饿汉单例模式:
```java
public class EagerSingleton {
private static final int POOL_SIZE = 3; // 连接池大小
private static EagerSingleton instance = new EagerSingleton(); // 饿汉式单例
private List<Connection> pool = new ArrayList<>(POOL_SIZE); // 连接池
private int count = 0; // 记录连接池中连接对象的数量
// 私有构造方法,初始化连接池
private EagerSingleton() {
for (int i = 0; i < POOL_SIZE; i++) {
pool.add(new Connection());
}
count = POOL_SIZE;
}
// 获取连接对象
public synchronized Connection getConnection() {
if (count > 0) {
Connection conn = pool.get(count - 1);
count--;
return conn;
} else {
return null;
}
}
// 归还连接对象
public synchronized void returnConnection(Connection conn) {
pool.add(conn);
count++;
}
// 获取单例对象
public static EagerSingleton getInstance() {
return instance;
}
}
class Connection {
// 省略连接数据库的操作
}
```
懒汉单例模式:
```java
public class LazySingleton {
private static int POOL_SIZE; // 连接池大小
private static LazySingleton instance; // 懒汉式单例
private List<Connection> pool; // 连接池
private int count = 0; // 记录连接池中连接对象的数量
// 私有构造方法,初始化连接池
private LazySingleton() {}
// 获取连接对象
public synchronized Connection getConnection() {
if (count > 0) {
Connection conn = pool.get(count - 1);
count--;
return conn;
} else {
return null;
}
}
// 归还连接对象
public synchronized void returnConnection(Connection conn) {
pool.add(conn);
count++;
}
// 获取单例对象
public static LazySingleton getInstance(int poolSize) {
POOL_SIZE = poolSize;
if (instance == null) {
synchronized (LazySingleton.class) {
if (instance == null) {
instance = new LazySingleton();
instance.pool = new ArrayList<>(POOL_SIZE);
for (int i = 0; i < POOL_SIZE; i++) {
instance.pool.add(new Connection());
}
instance.count = POOL_SIZE;
}
}
}
return instance;
}
}
class Connection {
// 省略连接数据库的操作
}
```
3. 文档说明
参考文献:无
注:本文档由AI自动生成,如有错误请及时联系我们。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)