当前位置:网站首页>建造者模式
建造者模式
2022-06-11 21:26:00 【小辣鸡~】
为什么需要构造者模式?
public class ResourcePoolConfig {
private static final int DEFAULT_MAX_TOTAL = 8;
private static final int DEFAULT_MAX_IDLE = 8;
private static final int DEFAULT_MIN_IDLE = 0;
private String name;
private int maxTotal = DEFAULT_MAX_TOTAL;
private int maxIdle = DEFAULT_MAX_IDLE;
private int minIdle = DEFAULT_MIN_IDLE;
public ResourcePoolConfig(String name) {
if (StringUtils.isBlank(name)) {
throw new IllegalArgumentException("name should not be empty.");
}
this.name = name;
}
public void setMaxTotal(int maxTotal) {
if (maxTotal <= 0) {
throw new IllegalArgumentException("maxTotal should be positive.");
}
this.maxTotal = maxTotal;
}
public void setMaxIdle(int maxIdle) {
if (maxIdle < 0) {
throw new IllegalArgumentException("maxIdle should not be negative.");
}
this.maxIdle = maxIdle;
}
public void setMinIdle(int minIdle) {
if (minIdle < 0) {
throw new IllegalArgumentException("minIdle should not be negative.");
}
this.minIdle = minIdle;
}
//...省略getter方法...
}
// ResourcePoolConfig使用举例
ResourcePoolConfig config = new ResourcePoolConfig("dbconnectionpool");
config.setMaxTotal(16);
config.setMaxIdle(8);
上述代码假如我们还要解决下面这三个问题:
1.我们刚刚讲的name是必填项,所以我们把它放到构造函数中,强制对象创建的时候就设置。但是如果必填项很多,就会导致参数列表很长,如果我们放到set方法中的话,那么校验这些必填项是否已经填写的逻辑就无处安放了。
2.除此之外,假设配置项之间有一定的依赖关系,比如,如果用户设置了 maxTotal、maxIdle、minIdle 其中一个,就必须显式地设置另外两个;或者配置项之间有一定的约束条件,比如,maxIdle 和 minIdle 要小于等于 maxTotal。如果我们继续使用现在的设计思路,那这些配置项之间的依赖关系或者约束条件的校验逻辑就无处安放了。
3.如果我们希望 ResourcePoolConfig 类对象是不可变对象,也就是说,对象在创建好之后,就不能再修改内部的属性值。要实现这个功能,我们就不能在 ResourcePoolConfig 类中暴露 set() 方法。
上述问题通过以下代码解决:
通过创建一个构建者内部类,然后构建者设置set方法,并且set方法的返回值是构建者,最后通过构造函数校验逻辑是否正确。
public class ResourcePoolConfig {
private String name;
private int maxTotal;
private int maxIdle;
private int minIdle;
private ResourcePoolConfig(Builder builder) {
this.name = builder.name;
this.maxTotal = builder.maxTotal;
this.maxIdle = builder.maxIdle;
this.minIdle = builder.minIdle;
}
//...省略getter方法...
//我们将Builder类设计成了ResourcePoolConfig的内部类。
//我们也可以将Builder类设计成独立的非内部类ResourcePoolConfigBuilder。
public static class Builder {
private static final int DEFAULT_MAX_TOTAL = 8;
private static final int DEFAULT_MAX_IDLE = 8;
private static final int DEFAULT_MIN_IDLE = 0;
private String name;
private int maxTotal = DEFAULT_MAX_TOTAL;
private int maxIdle = DEFAULT_MAX_IDLE;
private int minIdle = DEFAULT_MIN_IDLE;
public ResourcePoolConfig build() {
// 校验逻辑放到这里来做,包括必填项校验、依赖关系校验、约束条件校验等
if (StringUtils.isBlank(name)) {
throw new IllegalArgumentException("...");
}
if (maxIdle > maxTotal) {
throw new IllegalArgumentException("...");
}
if (minIdle > maxTotal || minIdle > maxIdle) {
throw new IllegalArgumentException("...");
}
return new ResourcePoolConfig(this);
}
public Builder setName(String name) {
if (StringUtils.isBlank(name)) {
throw new IllegalArgumentException("...");
}
this.name = name;
return this;
}
public Builder setMaxTotal(int maxTotal) {
if (maxTotal <= 0) {
throw new IllegalArgumentException("...");
}
this.maxTotal = maxTotal;
return this;
}
public Builder setMaxIdle(int maxIdle) {
if (maxIdle < 0) {
throw new IllegalArgumentException("...");
}
this.maxIdle = maxIdle;
return this;
}
public Builder setMinIdle(int minIdle) {
if (minIdle < 0) {
throw new IllegalArgumentException("...");
}
this.minIdle = minIdle;
return this;
}
}
}
// 这段代码会抛出IllegalArgumentException,因为minIdle>maxIdle
ResourcePoolConfig config = new ResourcePoolConfig.Builder()
.setName("dbconnectionpool")
.setMaxTotal(16)
.setMaxIdle(10)
.setMinIdle(12)
.build();
边栏推荐
- Serval and Rooted Tree(CF1153D)-DP
- AC自动机
- JVM之对象创建过程
- LeetCode-32-最长有效括号
- One article to show you how to understand the harmonyos application on the shelves
- 一步步把 SAP UI5 应用部署到 SAP BTP Kyma 运行环境中去
- 2022年6月9日 16:29:41 日记
- Using the sap ui5 cli command line tool to build and run SAP ui5 applications
- 如何创建最简单的 SAP Kyma Function
- LeetCode-322-零钱兑换
猜你喜欢

Part II data link layer

Tensorflow 2. X Getting Started tutorial

LeetCode-155-最小栈

LeetCode-76-最小覆盖子串

Network security Kali penetration learning introduction to web penetration using MSF penetration to attack win7 host and execute commands remotely

JVM object allocation policy TLAB

Answer fans' questions | count the number and frequency of letters in the text

JS performs non empty judgment on various data types of the returned data.

Add personal statement for go file in file template in Golan

LeetCode-104-二叉树的最大深度
随机推荐
JVM heap
RANSAC extraction plane (matlab built-in function)
String copy function
JVM|运行时数据区;程序计数器(PC寄存器);
八、BOM - 章节课后练习题及答案
Solve the problem of img 5px spacing
select _ Lazy loading
Educational Codeforces Round 114 (Rated for Div. 2) D
Regular check matches positive integer or decimal limit between [0-100] and [0-1000]
Using the sap ui5 cli command line tool to build and run SAP ui5 applications
Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:
LeetCode-155-最小栈
Go language functions
Serval and Rooted Tree(CF1153D)-DP
JVM|类加载器;双亲委派机制
Go语言函数
Part I physical layer
ASCII码对照表
Codeforces Round #744 (Div. 3) 解题报告
数据库每日一题---第9天:销售员