一分钟教会你使用Builder构建链式调用

简介: 一分钟教会你使用Builder构建链式调用

引言

大家好,小面又和大家见面了。相信大家都有可能用到链式调用,比如单元测中的使用:

@Test
    public void test() {
        // 连续使用多个eq()的情况
        LambdaQueryChainWrapper wrapper1 = PowerMockito.mock(LambdaQueryChainWrapper.class);
        LambdaQueryChainWrapper wrapper2 = PowerMockito.mock(LambdaQueryChainWrapper.class);
        LambdaQueryChainWrapper wrapper3 = PowerMockito.mock(LambdaQueryChainWrapper.class);
        LambdaQueryChainWrapper wrapper4 = PowerMockito.mock(LambdaQueryChainWrapper.class);
        LambdaQueryChainWrapper wrapper5 = PowerMockito.mock(LambdaQueryChainWrapper.class);
        List list = PowerMockito.mock(ArrayList.class);
        PowerMockito.doReturn(wrapper1).when(resourceSubOrderRepository).lambdaQuery();
        // eq ()三个参数的情况 ,anyBoolean() 匹配布尔型 
        PowerMockito.doReturn(wrapper2).when(wrapper1).eq(anyBoolean(), any(), any());
        PowerMockito.doReturn(wrapper3).when(wrapper2).eq(anyBoolean(), any(), any());
        PowerMockito.doReturn(wrapper4).when(wrapper3).eq(any(), any());
        PowerMockito.doReturn(wrapper5).when(wrapper4).eq(any(), any());
        PowerMockito.doReturn(list).when(wrapper5).list();
        ResponseResult > xx = controller.getInstanceByProductAndRegion(new UserBO(), "00", "xx", "111");
        System.out.println(xx.getData());
        Assert.assertEquals(new ArrayList<>(), xx.getData());
    }

我们可以看到有这种.doXXX.whenXXX.eqXXX这种链式调用法,接下来小面就带你1分钟学会自己写链式调用。

构建对象属性

大家日常在开发创建对象必然都会使用get、set方法。

比如我们创建一个Person类,里面有关于他的多个属性,并写好get、set方法。

public class Person {
    private String name;
    private Integer age;
    private String  card;
    private Integer mathScore;
    private Integer chineseScore;
    private Integer chemistryScore;
    private Integer physicalScore;
    private Integer biologicalScore;
    private Integer politicalScore;
    private Integer historicalScore;
    private Integer geographyScore;
    private Integer englishScore;
    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;
    }
    public String getCard() {
        return card;
    }
    public void setCard(String card) {
        this.card = card;
    }
    public Integer getMathScore() {
        return mathScore;
    }
    public void setMathScore(Integer mathScore) {
        this.mathScore = mathScore;
    }
    public Integer getChineseScore() {
        return chineseScore;
    }
    public void setChineseScore(Integer chineseScore) {
        this.chineseScore = chineseScore;
    }
    public Integer getChemistryScore() {
        return chemistryScore;
    }
    public void setChemistryScore(Integer chemistryScore) {
        this.chemistryScore = chemistryScore;
    }
    public Integer getPhysicalScore() {
        return physicalScore;
    }
    public void setPhysicalScore(Integer physicalScore) {
        this.physicalScore = physicalScore;
    }
    public Integer getBiologicalScore() {
        return biologicalScore;
    }
    public void setBiologicalScore(Integer biologicalScore) {
        this.biologicalScore = biologicalScore;
    }
    public Integer getPoliticalScore() {
        return politicalScore;
    }
    public void setPoliticalScore(Integer politicalScore) {
        this.politicalScore = politicalScore;
    }
    public Integer getHistoricalScore() {
        return historicalScore;
    }
    public void setHistoricalScore(Integer historicalScore) {
        this.historicalScore = historicalScore;
    }
    public Integer getGeographyScore() {
        return geographyScore;
    }
    public void setGeographyScore(Integer geographyScore) {
        this.geographyScore = geographyScore;
    }
    public Integer getEnglishScore() {
        return englishScore;
    }
    public void setEnglishScore(Integer englishScore) {
        this.englishScore = englishScore;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", card='" + card + '\'' +
                ", mathScore=" + mathScore +
                ", chineseScore=" + chineseScore +
                ", chemistryScore=" + chemistryScore +
                ", physicalScore=" + physicalScore +
                ", biologicalScore=" + biologicalScore +
                ", politicalScore=" + politicalScore +
                ", historicalScore=" + historicalScore +
                ", geographyScore=" + geographyScore +
                ", englishScore=" + englishScore +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name) && Objects.equals(age, person.age) && Objects.equals(card, person.card) && Objects.equals(mathScore, person.mathScore) && Objects.equals(chineseScore, person.chineseScore) && Objects.equals(chemistryScore, person.chemistryScore) && Objects.equals(physicalScore, person.physicalScore) && Objects.equals(biologicalScore, person.biologicalScore) && Objects.equals(politicalScore, person.politicalScore) && Objects.equals(historicalScore, person.historicalScore) && Objects.equals(geographyScore, person.geographyScore) && Objects.equals(englishScore, person.englishScore);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age, card, mathScore, chineseScore, chemistryScore, physicalScore, biologicalScore, politicalScore, historicalScore, geographyScore, englishScore);
    }
}

我们在给它设置属性时,使用set方法

public static void main(String[] args) {
        Person person = new Person();
        person.setAge(15);
        person.setCard("12345");
        person.setBiologicalScore(50);
        person.setChemistryScore(20);
        person.setChineseScore(100);
        person.setGeographyScore(80);
        person.setHistoricalScore(90);
        person.setEnglishScore(10);
        person.setMathScore(99);
    }

有多少个属性,我们就得手写多少次对吧,如果属性的数量比较多,那么就会写很多,有没有一种舒服点的姿势呢?

lombok的链式调用

那就是链式调用,我们先来直接使用lombok提供的方式。

首先还是创建Person类,添加lombok相关注解

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Person2 {
    private String name;
    private Integer age;
    private String  card;
    private Integer mathScore;
    private Integer chineseScore;
    private Integer chemistryScore;
    private Integer physicalScore;
    private Integer biologicalScore;
    private Integer politicalScore;
    private Integer historicalScore;
    private Integer geographyScore;
    private Integer englishScore;
}

创建对象赋予属性

public static void main(String[] args) {
        //链式调用
        Person2.Person2Builder builder = Person2.builder();
        Person2 p2 = builder.age(15)
                .englishScore(100)
                .biologicalScore(100)
                .card("12345")
                .geographyScore(100)
                .historicalScore(100)
                .chineseScore(100)
                .build();
        Integer age = p2.getAge();
    }

我们可以看到效果就是在直观视觉上看起来它的属性更规整,并且是一条链式语句,使用起来就更方便。

public static void main(String[] args) {
        Person person = new Person();
        person.setAge(15);
        person.setCard("12345");
        person.setBiologicalScore(50);
        person.setChemistryScore(20);
        person.setChineseScore(100);
        person.setGeographyScore(80);
        person.setHistoricalScore(90);
        person.setEnglishScore(10);
        person.setMathScore(99);
        //链式调用
        Person2.Person2Builder builder = Person2.builder();
        Person2 p2 = builder.age(15)
                .englishScore(100)
                .biologicalScore(100)
                .card("12345")
                .geographyScore(100)
                .historicalScore(100)
                .chineseScore(100)
                .build();
        Integer age = p2.getAge();
    }

两种使用方式的的对比显而易见,lombok是靠这个@Builder注解实现对应链式调用,接下来下面也就这个builder实现一下。

自己手动实现链式调用

首先还是创建Person类,添加set、get方法,增加有参无参构造方法

public class Person3 {
    private String name;
    private Integer age;
    private String card;
    private Integer mathScore;
    private Integer chineseScore;
    private Integer chemistryScore;
    private Integer physicalScore;
    private Integer biologicalScore;
    private Integer politicalScore;
    private Integer historicalScore;
    private Integer geographyScore;
    private Integer englishScore;
    public Person3() {
    }
    public Person3(String name, Integer age, String card, Integer mathScore, Integer chineseScore, Integer chemistryScore, Integer physicalScore, Integer biologicalScore, Integer politicalScore, Integer historicalScore, Integer geographyScore, Integer englishScore) {
        this.name = name;
        this.age = age;
        this.card = card;
        this.mathScore = mathScore;
        this.chineseScore = chineseScore;
        this.chemistryScore = chemistryScore;
        this.physicalScore = physicalScore;
        this.biologicalScore = biologicalScore;
        this.politicalScore = politicalScore;
        this.historicalScore = historicalScore;
        this.geographyScore = geographyScore;
        this.englishScore = englishScore;
    }
    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;
    }
    public String getCard() {
        return card;
    }
    public void setCard(String card) {
        this.card = card;
    }
    public Integer getMathScore() {
        return mathScore;
    }
    public void setMathScore(Integer mathScore) {
        this.mathScore = mathScore;
    }
    public Integer getChineseScore() {
        return chineseScore;
    }
    public void setChineseScore(Integer chineseScore) {
        this.chineseScore = chineseScore;
    }
    public Integer getChemistryScore() {
        return chemistryScore;
    }
    public void setChemistryScore(Integer chemistryScore) {
        this.chemistryScore = chemistryScore;
    }
    public Integer getPhysicalScore() {
        return physicalScore;
    }
    public void setPhysicalScore(Integer physicalScore) {
        this.physicalScore = physicalScore;
    }
    public Integer getBiologicalScore() {
        return biologicalScore;
    }
    public void setBiologicalScore(Integer biologicalScore) {
        this.biologicalScore = biologicalScore;
    }
    public Integer getPoliticalScore() {
        return politicalScore;
    }
    public void setPoliticalScore(Integer politicalScore) {
        this.politicalScore = politicalScore;
    }
    public Integer getHistoricalScore() {
        return historicalScore;
    }
    public void setHistoricalScore(Integer historicalScore) {
        this.historicalScore = historicalScore;
    }
    public Integer getGeographyScore() {
        return geographyScore;
    }
    public void setGeographyScore(Integer geographyScore) {
        this.geographyScore = geographyScore;
    }
    public Integer getEnglishScore() {
        return englishScore;
    }
    public void setEnglishScore(Integer englishScore) {
        this.englishScore = englishScore;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", card='" + card + '\'' +
                ", mathScore=" + mathScore +
                ", chineseScore=" + chineseScore +
                ", chemistryScore=" + chemistryScore +
                ", physicalScore=" + physicalScore +
                ", biologicalScore=" + biologicalScore +
                ", politicalScore=" + politicalScore +
                ", historicalScore=" + historicalScore +
                ", geographyScore=" + geographyScore +
                ", englishScore=" + englishScore +
                '}';
    }
}

接着在Person3中添加一个静态内部类Builder,添加上Person3中的所有属性

public static class Builder{
        private String name;
        private Integer age;
        private String  card;
        private Integer mathScore;
        private Integer chineseScore;
        private Integer chemistryScore;
        private Integer physicalScore;
        private Integer biologicalScore;
        private Integer politicalScore;
        private Integer historicalScore;
        private Integer geographyScore;
        private Integer englishScore;
    }

然后创建一个方法,方法名就是属性字段名如name,添加对应参数String name,赋值name,返回Builder自身

public Builder name(String name){
            this.name = name;
            return this;
        }

其他属性也都如此

public static class Builder{
        private String name;
        private Integer age;
        private String  card;
        private Integer mathScore;
        private Integer chineseScore;
        private Integer chemistryScore;
        private Integer physicalScore;
        private Integer biologicalScore;
        private Integer politicalScore;
        private Integer historicalScore;
        private Integer geographyScore;
        private Integer englishScore;
        public Builder name(String name){
            this.name = name;
            return this;
        }
        public Builder age(Integer age){
            this.age = age;
            return this;
        }
        public Builder card(String card){
            this.card = card;
            return this;
        }
        public Builder mathScore(Integer mathScore){
            this.mathScore = mathScore;
            return this;
        }
        public Builder chineseScore(Integer chineseScore){
            this.chineseScore = chineseScore;
            return this;
        }
        public Builder chemistryScore(Integer chemistryScore){
            this.chemistryScore = chemistryScore;
            return this;
        }
        public Builder physicalScore(Integer physicalScore){
            this.physicalScore = physicalScore;
            return this;
        }
        public Builder biologicalScore(Integer biologicalScore){
            this.biologicalScore = biologicalScore;
            return this;
        }
        public Builder politicalScore(Integer politicalScore){
            this.politicalScore = politicalScore;
            return this;
        }
        public Builder historicalScore(Integer historicalScore){
            this.historicalScore = historicalScore;
            return this;
        }
        public Builder geographyScore(Integer geographyScore){
            this.geographyScore = geographyScore;
            return this;
        }
        public Builder englishScore(Integer englishScore){
            this.englishScore = englishScore;
            return this;
        }
    }

创建build()方法返回Person3

public Person3 build(){
            return new Person3(this.name,this.age,this.card,this.mathScore,this.chineseScore,this.chemistryScore,this.physicalScore,this.biologicalScore,this.politicalScore,this.historicalScore,this.geographyScore,this.englishScore);
        }

这个时候已经构建完成了

public class Person3 {
    private String name;
    private Integer age;
    private String card;
    private Integer mathScore;
    private Integer chineseScore;
    private Integer chemistryScore;
    private Integer physicalScore;
    private Integer biologicalScore;
    private Integer politicalScore;
    private Integer historicalScore;
    private Integer geographyScore;
    private Integer englishScore;
    public Person3() {
    }
    public Person3(String name, Integer age, String card, Integer mathScore, Integer chineseScore, Integer chemistryScore, Integer physicalScore, Integer biologicalScore, Integer politicalScore, Integer historicalScore, Integer geographyScore, Integer englishScore) {
        this.name = name;
        this.age = age;
        this.card = card;
        this.mathScore = mathScore;
        this.chineseScore = chineseScore;
        this.chemistryScore = chemistryScore;
        this.physicalScore = physicalScore;
        this.biologicalScore = biologicalScore;
        this.politicalScore = politicalScore;
        this.historicalScore = historicalScore;
        this.geographyScore = geographyScore;
        this.englishScore = englishScore;
    }
    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;
    }
    public String getCard() {
        return card;
    }
    public void setCard(String card) {
        this.card = card;
    }
    public Integer getMathScore() {
        return mathScore;
    }
    public void setMathScore(Integer mathScore) {
        this.mathScore = mathScore;
    }
    public Integer getChineseScore() {
        return chineseScore;
    }
    public void setChineseScore(Integer chineseScore) {
        this.chineseScore = chineseScore;
    }
    public Integer getChemistryScore() {
        return chemistryScore;
    }
    public void setChemistryScore(Integer chemistryScore) {
        this.chemistryScore = chemistryScore;
    }
    public Integer getPhysicalScore() {
        return physicalScore;
    }
    public void setPhysicalScore(Integer physicalScore) {
        this.physicalScore = physicalScore;
    }
    public Integer getBiologicalScore() {
        return biologicalScore;
    }
    public void setBiologicalScore(Integer biologicalScore) {
        this.biologicalScore = biologicalScore;
    }
    public Integer getPoliticalScore() {
        return politicalScore;
    }
    public void setPoliticalScore(Integer politicalScore) {
        this.politicalScore = politicalScore;
    }
    public Integer getHistoricalScore() {
        return historicalScore;
    }
    public void setHistoricalScore(Integer historicalScore) {
        this.historicalScore = historicalScore;
    }
    public Integer getGeographyScore() {
        return geographyScore;
    }
    public void setGeographyScore(Integer geographyScore) {
        this.geographyScore = geographyScore;
    }
    public Integer getEnglishScore() {
        return englishScore;
    }
    public void setEnglishScore(Integer englishScore) {
        this.englishScore = englishScore;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", card='" + card + '\'' +
                ", mathScore=" + mathScore +
                ", chineseScore=" + chineseScore +
                ", chemistryScore=" + chemistryScore +
                ", physicalScore=" + physicalScore +
                ", biologicalScore=" + biologicalScore +
                ", politicalScore=" + politicalScore +
                ", historicalScore=" + historicalScore +
                ", geographyScore=" + geographyScore +
                ", englishScore=" + englishScore +
                '}';
    }
    public static class Builder{
        private String name;
        private Integer age;
        private String  card;
        private Integer mathScore;
        private Integer chineseScore;
        private Integer chemistryScore;
        private Integer physicalScore;
        private Integer biologicalScore;
        private Integer politicalScore;
        private Integer historicalScore;
        private Integer geographyScore;
        private Integer englishScore;
        public Builder name(String name){
            this.name = name;
            return this;
        }
        public Builder age(Integer age){
            this.age = age;
            return this;
        }
        public Builder card(String card){
            this.card = card;
            return this;
        }
        public Builder mathScore(Integer mathScore){
            this.mathScore = mathScore;
            return this;
        }
        public Builder chineseScore(Integer chineseScore){
            this.chineseScore = chineseScore;
            return this;
        }
        public Builder chemistryScore(Integer chemistryScore){
            this.chemistryScore = chemistryScore;
            return this;
        }
        public Builder physicalScore(Integer physicalScore){
            this.physicalScore = physicalScore;
            return this;
        }
        public Builder biologicalScore(Integer biologicalScore){
            this.biologicalScore = biologicalScore;
            return this;
        }
        public Builder politicalScore(Integer politicalScore){
            this.politicalScore = politicalScore;
            return this;
        }
        public Builder historicalScore(Integer historicalScore){
            this.historicalScore = historicalScore;
            return this;
        }
        public Builder geographyScore(Integer geographyScore){
            this.geographyScore = geographyScore;
            return this;
        }
        public Builder englishScore(Integer englishScore){
            this.englishScore = englishScore;
            return this;
        }
        public Person3 build(){
            return new Person3(this.name,this.age,this.card,this.mathScore,this.chineseScore,this.chemistryScore,this.physicalScore,this.biologicalScore,this.politicalScore,this.historicalScore,this.geographyScore,this.englishScore);
        }
    }
}

直接使用

public static void main(String[] args) {
        //自己实现builder
        Person3 p3 = new Person3.Builder()
                .age(18)
                .card("12345")
                .mathScore(100)
                .chemistryScore(100)
                .biologicalScore(100)
                .build();
        Integer p3Age = p3.getAge();
    }

可以看到效果和lombok实现的方式一样,我们自己也可以链式调用自己的对象了。

总结

小面今天的教学到这就结束啦,相信你已经学会了自己如何构建链式调用的方法,后面代码实操可以用起来,能在工作中对你有所帮助的。

相关文章
|
1月前
|
API Python
当调用一个 Python 对象时,背后都经历了哪些过程?
当调用一个 Python 对象时,背后都经历了哪些过程?
22 2
|
6月前
|
设计模式 算法 中间件
【C++ 可调用对象的应用】C++设计模式与现代编程技巧:深入可调用对象的世界
【C++ 可调用对象的应用】C++设计模式与现代编程技巧:深入可调用对象的世界
212 1
|
6月前
|
设计模式 算法
一步步构建完美对象:建造者模式的实战应用
建造者模式是一种设计模式,用于创建复杂对象。它允许你按照特定的步骤构造对象,同时可以隐藏创建细节,使得用户无需知道内部构建过程。这种模式适用于需要构建多个不同属性配置的对象,例如创建一个复杂的产品对象,或者在构建对象时涉及多个步骤的情况。优点: 分离构建过程和最终表示:建造者模式可以将产品的构建过程与表示分离,使得相同的构建过程可以得到不同的表示。 易于扩展:通过建造者模式,可以很容易地扩展和改变产品的内部表示,使得我们可以更灵活地修改构建过程。 隐藏细节:客户端不需要关心产品的构建细节,只需关心最终的产
106 2
|
存储 编译器 C++
【C++】list迭代器的深度剖析及模拟实现(感受类封装,类和对象的思想)
【C++】list迭代器的深度剖析及模拟实现(感受类封装,类和对象的思想)
|
设计模式 Java API
「 Java基础-链式调用 」Java开发中如何让你的代码看起来更优雅?试试链式调用?
我们日常在写业务代码的时候,经常会遇到一种场景,比如一个对象有很多属性,比如用户对象有很多属性:用户名、用户ID、用户性别、用户居住地址、用户工作类型、用户联系方式等等,当我们要构建一个用户对象的时候,就要不断的去`set,get`
「 Java基础-链式调用 」Java开发中如何让你的代码看起来更优雅?试试链式调用?
|
jenkins 测试技术 持续交付
python接口自动化(三十六)-封装与调用--流程类接口关联续集(详解)
上一篇已经给大家都介绍过了流程类接口关联,但是由于博客的登录机制改变,所以没有办法给小伙伴们实战演练一下,那么这篇就按照上一篇计划的用jenkins来给小伙伴们演示一下流程类接口的封装和调用,其实很简单,就是用上一篇和前边这篇传送门的代码稍稍修改即可。不知道你自己练习了么,如果练习了,可以看看和我有什么不同,没练习的看看自己思路和我有啥不一样。好了废话少说进入主题
201 0
python接口自动化(三十六)-封装与调用--流程类接口关联续集(详解)
|
jenkins 测试技术 持续交付
python接口自动化(三十五)-封装与调用--流程类接口关联(详解)
流程相关的接口,主要用 session 关联,如果写成函数(如上篇),s 参数每个函数都要带,每个函数多个参数,这时候封装成类会更方便。在这里我们还是以博客园为例,带着小伙伴们实践一下。
387 0
python接口自动化(三十五)-封装与调用--流程类接口关联(详解)
|
Python 容器
【Python零基础入门篇 · 30】:自定义迭代器类、异常类、生成器
【Python零基础入门篇 · 30】:自定义迭代器类、异常类、生成器
107 0
【Python零基础入门篇 · 30】:自定义迭代器类、异常类、生成器
|
Python 容器
【Python零基础入门篇 · 20】:可迭代对象和迭代器的转换、自定义迭代器类、异常类、生成器
【Python零基础入门篇 · 20】:可迭代对象和迭代器的转换、自定义迭代器类、异常类、生成器
142 0
【Python零基础入门篇 · 20】:可迭代对象和迭代器的转换、自定义迭代器类、异常类、生成器
|
Java 大数据 Scala
scala 对象创建的流程分析|学习笔记
快速学习 scala 对象创建的流程分析。
106 0