Package org.miaixz.bus.core.builder
Class GenericBuilder<T>
java.lang.Object
org.miaixz.bus.core.builder.GenericBuilder<T>
- Type Parameters:
T- 构建对象类型
- All Implemented Interfaces:
Serializable,Builder<T>
通用Builder 参考: 一看就会的java8通用Builder
使用方法如下:
Box box = GenericBuilder.of(Box::new).with(Box::setId, 1024L).with(Box::setTitle, "Hello World!")
.with(Box::setLength, 9).with(Box::setWidth, 8).with(Box::setHeight, 7).build();
我们也可以对已创建的对象进行修改:
Box boxModified = GenericBuilder.of(() -> box).with(Box::setTitle, "Hello Friend!").with(Box::setLength, 3)
.with(Box::setWidth, 4).with(Box::setHeight, 5).build();
我们还可以对这样调用有参构造,这对于创建一些在有参构造中包含初始化函数的对象是有意义的:
Box box1 = GenericBuilder.of(Box::new, 2048L, "Hello Partner!", 222, 333, 444).with(Box::alis).build();
还可能这样构建Map对象:
HashMap<String, String> colorMap = GenericBuilder
.of(HashMap<String,String>::new)
.with(Map::put, "red", "#FF0000")
.with(Map::put, "yellow", "#FFFF00")
.with(Map::put, "blue", "#0000FF")
.build();
注意:本工具类支持调用的构造方法的参数数量不超过5个,一般方法的参数数量不超过2个,更多的参数不利于阅读和维护
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()构建static <T> GenericBuilder<T> 通过Supplier创建GenericBuilder<P1> GenericBuilder<T> with(BiConsumer<T, P1> consumer, P1 p1) 调用1参数方法调用无参数方法<P1,P2> GenericBuilder <T> with(SerConsumer3<T, P1, P2> consumer, P1 p1, P2 p2) 调用2参数方法
-
Constructor Details
-
GenericBuilder
构造- Parameters:
instant- 实例化器
-
-
Method Details
-
of
通过Supplier创建GenericBuilder- Type Parameters:
T- 目标类型- Parameters:
instant- 实例化器- Returns:
- GenericBuilder对象
-
with
调用无参数方法- Parameters:
consumer- 无参数Consumer- Returns:
- GenericBuilder对象
-
with
调用1参数方法- Type Parameters:
P1- 参数一类型- Parameters:
consumer- 1参数Consumerp1- 参数一- Returns:
- GenericBuilder对象
-
with
调用2参数方法- Type Parameters:
P1- 参数一类型P2- 参数二类型- Parameters:
consumer- 2参数Consumerp1- 参数一p2- 参数二- Returns:
- GenericBuilder对象
-
build
构建
-