当前位置:网站首页>Builder pattern
Builder pattern
2022-06-11 21:33:00 【Spicy chicken~】
Why the constructor pattern is needed ?
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;
}
//... Omit getter Method ...
}
// ResourcePoolConfig Use examples
ResourcePoolConfig config = new ResourcePoolConfig("dbconnectionpool");
config.setMaxTotal(16);
config.setMaxIdle(8);
If we still need to solve the following three problems in the above code :
1. What we just said name Are mandatory , So we put it in the constructor , Force objects to be set when they are created . But if there are many required items , This will result in a long parameter list , If we put set Method , Then the logic to check whether these required items have been filled in is nowhere to be placed .
2. besides , Assume that there are certain dependencies between configuration items , such as , If the user has set maxTotal、maxIdle、minIdle One of them , You must explicitly set the other two ; Or there are certain constraints between configuration items , such as ,maxIdle and minIdle Less than or equal to maxTotal. If we continue to use the current design ideas , Then there is no place for the verification logic of the dependencies or constraints between these configuration items .
3. If we want to ResourcePoolConfig Class objects are immutable objects , in other words , After the object is created , You can no longer modify the internal attribute value . To implement this function , We can't be in ResourcePoolConfig Exposure in class set() Method .
The above problem is solved by the following code :
By creating a builder inner class , Then the builder sets set Method , also set The return value of the method is the constructor , Finally, check whether the logic is correct through the constructor .
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;
}
//... Omit getter Method ...
// We will Builder Class is designed to ResourcePoolConfig The inner class of .
// We can also put Builder Class is designed as a separate non inner class 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() {
// Put the verification logic here to do , Including required items 、 Dependency verification 、 Constraint verification, etc
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;
}
}
}
// This code will throw IllegalArgumentException, because minIdle>maxIdle
ResourcePoolConfig config = new ResourcePoolConfig.Builder()
.setName("dbconnectionpool")
.setMaxTotal(16)
.setMaxIdle(10)
.setMinIdle(12)
.build();
边栏推荐
- Codeforces Round #740 Div. 2 解题报告
- Answer fans' questions | count the number and frequency of letters in the text
- RPA超自动化 | 农耕记携手云扩加速财务智能化运营
- LabVIEW控制Arduino实现超声波测距(进阶篇—5)
- How to manually send events exposed by SAP commerce cloud mock application using SAP kyma console
- 二分图King
- LabVIEW控制Arduino实现红外测距(进阶篇—6)
- Redis transaction
- Add personal statement for go file in file template in Golan
- LeetCode-98-验证二叉搜索树
猜你喜欢

即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍

How to manually drag nodes in the Obsidian relationship graph

Flutter implements the JD address selection component

js对返回的数据的各种数据类型进行非空判断。

Leetcode-76- minimum covering substring

LeetCode-104-二叉树的最大深度

A collection of commonly used open source data sets for face recognition

Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience

Leetcode-43- string multiplication

How to Load Data from CSV (Data Preparation Part)
随机推荐
LabVIEW Arduino电子称重系统(项目篇—1)
八、BOM - 章节课后练习题及答案
2021牛客多校5 Double Strings
12 golden rules of growth
Educational codeforces round 111 (rated for Div. 2) C Supplement
即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
Go language for loop
A collection of commonly used open source data sets for face recognition
Go language functions
Refresh and upgrade | innovation, starting from cloud store
Syntax of SQL
JVM | runtime data area; Program counter (PC register);
LabVIEW控制Arduino实现超声波测距(进阶篇—5)
Educational Codeforces Round 111 (Rated for Div. 2) C 补题
LeetCode-129-求根节点到叶节点数字之和
Cs144 lab0 lab1 record
实现 TabLayout 下标与文字等长,选中字体大小改变
Bug -- coredump usage
Redis data type (string)
As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?