前言
java8是14年发布的,已经走过了6个多年头了,但是里面的一些宝贝还没被我挖掘出来并使用,前几天看了一篇博客,了解到:
Java8内置了四大核心函数式接口:
Consumer :消费型接口 void accept(T t);
Supplier :供给型接口: T get();
Function<T,R> :函数型接口: R apply(T t);
Predicate :断言型接口: boolean test(T t);
根据前两个接口可以衍生出一个通用的Builder,我是参考的博客如下:
一看就会的java8通用Builder
自己也实践了一下,也加入了自己想要实现的内容
一、消费型接口和供给型接口小测验
1、Supplier小demo
package com.fanhf.javastudy.builderreplcnew;
import lombok.*;
import java.util.function.Supplier;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
/**
* @author fanhf
* @Description Supplier :这个接口是用来创建对象,特点是懒加载
* @date 2021-01-21 10:45
*/
public class SupplierTest {
private String name;
private String address;
private String country;
public static void main(String[] args) {
//创建Supplier的容器,声明为SupplierTest类型,此时并不会调用对象的构造方法,即不会创建对象
Supplier<SupplierTest> supplier = SupplierTest::new;
//调用get方法,才会获取真正的对象
System.out.println(supplier.get());
}
}
输出:
SupplierTest(super=com.fanhf.javastudy.builderreplcnew.SupplierTest@5750a, name=null, address=null, country=null)
2、Consumer的小demo
package com.fanhf.javastudy.builderreplcnew;
import java.util.function.Consumer;
/**
* @author fanhf
* @Description 对象消费者
* @date 2021-01-21 10:45
*/
public class ConsumerTest {
public static void main(String[] args) {
Consumer<Integer> consumer = (x) -> {
int num = x * 10;
System.out.println(num);
};
Consumer<Integer> consumer1 = (x) -> {
int num = x * 20;
System.out.println(num);
};
consumer.andThen(consumer1).accept(1);
}
}
输出:
10
20
二、自定义通用Builder类
这个是通用的 Builder 模式构建器,也是最核心的部分
package com.fanhf.javastudy.builderreplcnew;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* @author fanhf
* @Description TODO
* @date 2021-01-21 10:50
*/
public class Builder<T> {
private final Supplier<T> instantiator;
private List<Consumer> modifiers = new ArrayList<>();
public Builder(Supplier<T> instantiator) {
this.instantiator = instantiator;
}
public static <T> Builder<T> of(Supplier<T> instant) {
return new Builder<>(instant);
}
/**
* 1、参数 Consumer
**/
@FunctionalInterface
public interface Consumer1<T, P1> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数二
**/
void accept(T t, P1 p1);
}
public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) {
Consumer<T> consumer1 = instantiator -> consumer.accept(instantiator, p1);
modifiers.add(consumer1);
return this;
}
/**
* 2、参数 Consumer
**/
@FunctionalInterface
public interface Consumer2<T, P1, P2> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
**/
void accept(T t, P1 p1, P2 p2);
}
public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
Consumer<T> consumer2 = instantiator -> consumer.accept(instantiator, p1, p2);
modifiers.add(consumer2);
return this;
}
/**
* 3、参数 Consumer
**/
@FunctionalInterface
public interface Consumer3<T, P1, P2, P3> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
**/
void accept(T t, P1 p1, P2 p2, P3 p3);
}
public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
Consumer<T> consumer3 = instantiator -> consumer.accept(instantiator, p1, p2, p3);
modifiers.add(consumer3);
return this;
}
/**
* 4、参数 Consumer
**/
@FunctionalInterface
public interface Consumer4<T, P1, P2, P3, P4> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
**/
void accept(T t, P1 p1, P2 p2, P3 p3, P4 p4);
}
public <P1, P2, P3, P4> Builder<T> with(Consumer4<T, P1, P2, P3, P4> consumer, P1 p1, P2 p2, P3 p3, P4 p4) {
Consumer<T> consumer4 = instantiator -> consumer.accept(instantiator, p1, p2, p3, p4);
modifiers.add(consumer4);
return this;
}
/**
* 5、参数 Consumer
**/
@FunctionalInterface
public interface Consumer5<T, P1, P2, P3, P4, P5> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
* @param p5 参数五
**/
void accept(T t, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
}
public <P1, P2, P3, P4, P5> Builder<T> with(Consumer5<T, P1, P2, P3, P4, P5> consumer, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
Consumer<T> consumer5 = instantiator -> consumer.accept(instantiator, p1, p2, p3, p4, p5);
modifiers.add(consumer5);
return this;
}
public T build() {
T value = instantiator.get();
modifiers.forEach(modifier -> modifier.accept(value));
modifiers.clear();
return value;
}
}
说明:我这里也写了5个参数,其实一般情况下3个参数应该是足够了,如果不够的话,可以照葫芦画瓢,再自行添加。
三、测试
先来几个Bean
1、School
package com.fanhf.javastudy.builderreplcnew.test;
import lombok.*;
/**
* @author fanhf
* @date 2021-01-25 09:56
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class School {
private String schoolName;
private String major;
}
2、Job
package com.fanhf.javastudy.builderreplcnew.test;
import lombok.*;
/**
* @author fanhf
* @date 2021-01-25 09:56
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class Job {
private String company;
private Double salary;
}
3、BasicInfo
package com.fanhf.javastudy.builderreplcnew.test;
import com.fanhf.javastudy.builderreplcnew.Builder;
import lombok.*;
/**
* @author fanhf
* @date 2021-01-25 10:16
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
public class BasicInfo {
private String name;
private Integer age;
private String address;
private Job job;
public void setJob(String company,Double salary){
this.job = Builder.of(Job::new)
.with(Job::setCompany,company)
.with(Job::setSalary,salary)
.build();
}
}
4、Person
package com.fanhf.javastudy.builderreplcnew.test;
import com.fanhf.javastudy.builderreplcnew.Builder;
import lombok.*;
/**
* @author fanhf
* @date 2021-01-25 09:56
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
public class Person {
private BasicInfo basicInfo;
private School school;
public void setBasicInfo(String name,Integer age,String address,String company,Double salary){
this.basicInfo = Builder.of(BasicInfo::new)
.with(BasicInfo::setAddress, address)
.with(BasicInfo::setAge, age)
.with(BasicInfo::setName, name)
.with(BasicInfo::setJob,company,salary)
.build();
}
public void setSchool(String schoolName,String major){
this.school = Builder.of(School::new)
.with(School::setSchoolName, schoolName)
.with(School::setMajor, major)
.build();
}
}
5、InvokeMain
package com.fanhf.javastudy.builderreplcnew.test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fanhf.javastudy.builderreplcnew.Builder;
/**
* @author fanhf
* @Description 测试主类
* @date 2021-01-25 10:32
*/
public class InvokeMain {
public static void main(String[] args) {
Person person = Builder.of(Person::new)
.with(Person::setBasicInfo,"fanhf",18,"Beijing China","BAT",2000.1)
.with(Person::setSchool,"Peking University","Computer")
.build();
System.out.println(JSON.toJSONString(person, SerializerFeature.SortField,SerializerFeature.PrettyFormat));
}
}
输出结果:
{
"basicInfo":{
"address":"Beijing China",
"age":18,
"job":{
"company":"BAT",
"salary":2000.1
},
"name":"fanhf"
},
"school":{
"major":"Computer",
"schoolName":"Peking University"
}
}
一定要自己亲手打一遍,再进行扩展,否则时间就白费了,
遇到new对象的实际开发时,将其运用到开发中,才能有所精进。
--------------你知道的越多,不知道的越多-----------