一、前言
最近学习了一下java8的语法,发现了一个比较好用的一对逆运算接口:
Supplier:这个接口是用来创建对象的,最大的特点是懒加载。
Supplier源码:
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
调用方法Demo:
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class Student{
private String alg;
private String value;
private String cert;
public static void main(String[] args) {
创建Supplier容器,声明为Supplier类型,此时并不会调用对象的构造方法,即不会创建对象
Supplier<Student> student= Student::new;
//调用get()方法,此时会调用对象的构造方法,即获得到真正对象
System.out.println(sign.get());
}
}
输出结果:Student(super=com.test.entity.Student@5653a, alg=null, value=null, cert=null)
Consumer:顾名思义,这是一个对象消费者
Consumer接口源码
@FunctionalInterface
public interface Consumer<T> {
//接收一个参数
void accept(T t);
//调用者方法执行完后再执行after的方法
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
调用方法Demo:
import java.util.function.Consumer;
public class ConsumerTest {
public static void main(String[] args) {
Consumer<Integer> consumer = (x)->{
int num = x * 1;
System.out.println(num);
};
Consumer<Integer> consumer1 = (x) -> {
int num = x * 2;
System.out.println(num);
};
consumer.andThen(consumer1).accept(1);
}
}
输出结果为:10 20,证明先执行consumer的方法,然后再执行consumer1的方法。
二、使用Supplier和Consumer构建java8通用Builder
Builder代码示例(核心类代码)
package mytest;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class Builder<T> {
private final Supplier<T> instantiator;
private List<Consumer<T>> modifiers = new ArrayList<>();
public Builder(Supplier<T> instantiator) {
this.instantiator = instantiator;
}
public static <T> Builder<T> of(Supplier<T> instantiator) {
return new Builder<>(instantiator);
}
public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) {
Consumer<T> c = instance -> consumer.accept(instance, p1);
modifiers.add(c);
return this;
}
public <P1, P2> Builder<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;
}
public <P1, P2, P3> Builder<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;
}
public T build() {
T value = instantiator.get();
modifiers.forEach(modifier -> modifier.accept(value));
modifiers.clear();
return value;
}
/**
* 1 参数 Consumer
*/
@FunctionalInterface
public interface Consumer1<T, P1> {
void accept(T t, P1 p1);
}
/**
* 2 参数 Consumer
*/
@FunctionalInterface
public interface Consumer2<T, P1, P2> {
void accept(T t, P1 p1, P2 p2);
}
/**
* 3 参数 Consumer
*/
@FunctionalInterface
public interface Consumer3<T, P1, P2, P3> {
void accept(T t, P1 p1, P2 p2, P3 p3);
}
}
Builder方法调用
先创建一个测试的对象
public class Student {
private String id;
private String name;
private String sex;
private Integer age;
private Date birthday;
//get、set、toString方法略
}
调用Builder方法Demo
public static void main(String[] args) {
Student student = Builder.of(Student::new).with(Student::setId, "1111").with(Student::setName, "张三").with(Student::setSex, "男")
.with(Student::setAge, 23)
.with(Student::setBirthday, new Date()).build();
log.info("Student:"+student.toString());
}
输出结果:
Student:Student(id=1111, name=张三, sex=男, age=23, birthday=Wed Jan 27 18:02:57 CST 2021)
Builder代码简单介绍:
- 通过Supplier懒加载的特性保存需要创建的对象
- 将每一步的操作保存到Consumer中,存入到List集合
- 在build方法中通过Supplier的get()方法获取实例,在遍历list调用每个Consumer的accept方法给该实例的参数进行初始化