title: JDK8 通用Builder
tags: 新建,模板,小书匠
renderNumberedHeading: true
grammar_cjkRuby: true
前提
Supplier(供应商)
package java.util.function;
/**
* Represents a supplier of results.
* 代表结果的提供者。
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
* 供应商被调用的时候不需要每次都返回一个新的或不同的结果
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #get()}.
*
* @param <T> the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
- Dome
public static void main(String[] args) {
class Dome{
String name;
Integer age =10;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Supplier<Dome> supplier = Dome::new;
supplier.get().setName("0_0");
System.err.println(supplier.get().getName());
System.err.println(supplier.get().getAge());
System.err.println("-------------------------");
Dome dome = supplier.get();
dome.setName("0^0");
System.err.println(dome.getName());
System.err.println(dome.getAge());
System.err.println("-------------------------");
}
result:
null
10
-------------------------
0^0
10
-------------------------
Consumer(消费者)
import java.util.Objects;
/**
* Represents an operation that accepts a single input argument and returns no
* result. Unlike most other functional interfaces, {@code Consumer} is expected
* to operate via side-effects.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #accept(Object)}.
*
* @param <T> the type of the input to the operation
*
* @since 1.8
*/
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
- 简单使用,便于理解
public static void main(String[] args) {
Consumer<String> c1 = x -> System.err.println(x);
Consumer<String> c2 = x -> System.err.println(x+"..> ok");
c1.accept("0_0");
System.err.println("-------------------------");
c1.andThen(c2).accept("0^0");
}
result:
0_0
-------------------------
0^0
0^0..> ok
Process finished with exit code 0
主体代码
/**
* @ClassName BuilderFactory
* @Description TODO
* @Author yefeng
* @Date 2021/1/28 下午3:49
**/
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* @ClassName BuilderFactory
* @Description 通用的 Builder 模式构建器
* @Author yefeng
* @Date 2021/1/28 下午4:04
**/
public class BuilderFactory<T> {
/**
* 容器
*/
private final Supplier<T> instantiator;
/**
* 消费集合
*/
private List<Consumer<T>> modifiers = new ArrayList<>();
/**
* @Author yefeng
* @Description: 构造方法
* @Date 下午4:28 2021/1/28
* @param instantiator
* @return
**/
public BuilderFactory(Supplier<T> instantiator) {
this.instantiator = instantiator;
}
/**
* @Author yefeng
* @Description: 实例化对象
* @Date 下午4:29 2021/1/28
* @param instantiator
* @return com.factory.builder.BuilderFactory<T>
**/
public static <T> BuilderFactory<T> of(Supplier<T> instantiator) {
return new BuilderFactory<>(instantiator);
}
/**
* @Author yefeng
* @Description: 调用一个参数方法
* @Date 下午4:30 2021/1/28
* @param consumer
* @param p1
* @return com.factory.builder.BuilderFactory<T>
**/
public <P1> BuilderFactory<T> with(Consumer1<T, P1> consumer, P1 p1) {
Consumer<T> c = instance -> consumer.accept(instance, p1);
modifiers.add(c);
return this;
}
/**
* @Author yefeng
* @Description: 调用两个参数方法
* @Date 下午4:30 2021/1/28
* @param consumer
* @param p1
* @return com.factory.builder.BuilderFactory<T>
**/
public <P1, P2> BuilderFactory<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2);
modifiers.add(c);
return this;
}
/**
* @Author yefeng
* @Description: 调用三个参数方法
* @Date 下午4:30 2021/1/28
* @param consumer
* @param p1
* @return com.factory.builder.BuilderFactory<T>
**/
public <P1, P2, P3> BuilderFactory<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3);
modifiers.add(c);
return this;
}
/**
* @Author yefeng
* @Description: 构建实例
* @Date 下午4:27 2021/1/28
* @param
* @return T
**/
public T build() {
T value = instantiator.get();
//循环赋值
//理解感受 Consumer 消费的原理
/**
*
* Consumer1<T,P1> = T::Method -> T::Method(P1 p1)
* Consumer<T> c = instance -> Consumer1.accept(instance, p1) => instance[T]::Method(p1)
*
*/
modifiers.forEach(modifier -> modifier.accept(value));
modifiers.clear();
return value;
}
/**
* @Author yefeng
* @Description: 一个参数
* @Date 下午4:26 2021/1/28
* @param
* @return
**/
@FunctionalInterface
public interface Consumer1<T, P1> {
void accept(T t, P1 p1);
}
/**
* @Author yefeng
* @Description: 两个参数
* @Date 下午4:26 2021/1/28
* @param
* @return
**/
@FunctionalInterface
public interface Consumer2<T, P1, P2> {
void accept(T t, P1 p1, P2 p2);
}
/**
* @Author yefeng
* @Description: 三个参数
* @Date 下午4:26 2021/1/28
* @param
* @return
**/
@FunctionalInterface
public interface Consumer3<T, P1, P2, P3> {
void accept(T t, P1 p1, P2 p2, P3 p3);
}
/**
* 以此类推 ...
*/