当前位置:网站首页>建造者模式
建造者模式
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();
边栏推荐
- Codeworks round 744 (Div. 3) problem solving Report
- How to Load Data from CSV (Data Preparation Part)
- Object creation process of JVM
- 正则校验匹配[0-100]、[0-1000]之间的正整数或小数点位数限制
- 字符串复制函数
- Bug -- coredump usage
- Educational Codeforces Round 111 (Rated for Div. 2) C 补题
- RANSAC提取平面(MATLAB内置函数)
- bzoj3188 Upit
- Answer fans' questions | count the number and frequency of letters in the text
猜你喜欢
随机推荐
Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:
Codeforces Round #740 Div. 2 解题报告
解决 img 5px 间距的问题
Codeworks round 739 (Div. 3) problem solving Report
Notes on the preload method of Gorm
Website online customer service system Gofly source code development log - 2 Develop command line applications
Live broadcast with practice | 30 minutes to build WordPress website with Alibaba cloud container service and container network file system
12 golden rules of growth
JVM运行时常量池以及直接内存
Cs144 lab0 lab1 record
JVM之对象创建过程
Go语言函数
Regular check matches positive integer or decimal limit between [0-100] and [0-1000]
Apache local multi port configuration
Codeworks round 740 Div. 2 problem solving Report
JVM method area
Educational codeforces round 111 (rated for Div. 2) C Supplement
LabVIEW控制Arduino实现红外测距(进阶篇—6)
Bug -- coredump usage
[advanced C language] integer storage in memory

![[advanced C language] integer storage in memory](/img/a5/6c7df3b8f427fe724922a6b853516f.png)






