掘金 阅读 ( ) • 2021-06-23 15:26
.markdown-body{word-break:break-word;line-height:1.75;font-weight:400;font-size:15px;overflow-x:hidden;color:#333}.markdown-body h1,.markdown-body h2,.markdown-body h3,.markdown-body h4,.markdown-body h5,.markdown-body h6{line-height:1.5;margin-top:35px;margin-bottom:10px;padding-bottom:5px}.markdown-body h1{font-size:30px;margin-bottom:5px}.markdown-body h2{padding-bottom:12px;font-size:24px;border-bottom:1px solid #ececec}.markdown-body h3{font-size:18px;padding-bottom:0}.markdown-body h4{font-size:16px}.markdown-body h5{font-size:15px}.markdown-body h6{margin-top:5px}.markdown-body p{line-height:inherit;margin-top:22px;margin-bottom:22px}.markdown-body img{max-width:100%}.markdown-body hr{border:none;border-top:1px solid #ddd;margin-top:32px;margin-bottom:32px}.markdown-body code{word-break:break-word;border-radius:2px;overflow-x:auto;background-color:#fff5f5;color:#ff502c;font-size:.87em;padding:.065em .4em}.markdown-body code,.markdown-body pre{font-family:Menlo,Monaco,Consolas,Courier New,monospace}.markdown-body pre{overflow:auto;position:relative;line-height:1.75}.markdown-body pre>code{font-size:12px;padding:15px 12px;margin:0;word-break:normal;display:block;overflow-x:auto;color:#333;background:#f8f8f8}.markdown-body a{text-decoration:none;color:#0269c8;border-bottom:1px solid #d1e9ff}.markdown-body a:active,.markdown-body a:hover{color:#275b8c}.markdown-body table{display:inline-block!important;font-size:12px;width:auto;max-width:100%;overflow:auto;border:1px solid #f6f6f6}.markdown-body thead{background:#f6f6f6;color:#000;text-align:left}.markdown-body tr:nth-child(2n){background-color:#fcfcfc}.markdown-body td,.markdown-body th{padding:12px 7px;line-height:24px}.markdown-body td{min-width:120px}.markdown-body blockquote{color:#666;padding:1px 23px;margin:22px 0;border-left:4px solid #cbcbcb;background-color:#f8f8f8}.markdown-body blockquote:after{display:block;content:""}.markdown-body blockquote>p{margin:10px 0}.markdown-body ol,.markdown-body ul{padding-left:28px}.markdown-body ol li,.markdown-body ul li{margin-bottom:0;list-style:inherit}.markdown-body ol li .task-list-item,.markdown-body ul li .task-list-item{list-style:none}.markdown-body ol li .task-list-item ol,.markdown-body ol li .task-list-item ul,.markdown-body ul li .task-list-item ol,.markdown-body ul li .task-list-item ul{margin-top:0}.markdown-body ol ol,.markdown-body ol ul,.markdown-body ul ol,.markdown-body ul ul{margin-top:3px}.markdown-body ol li{padding-left:6px}.markdown-body .contains-task-list{padding-left:0}.markdown-body .task-list-item{list-style:none}@media (max-width:720px){.markdown-body h1{font-size:24px}.markdown-body h2{font-size:20px}.markdown-body h3{font-size:18px}}

这是我参与更文挑战的第23天,活动详情查看:更文挑战


👉设计模式目录

一、什么是构建器模式
1.概念

构建器( Builder )模式,有的也叫建造者模式。 构建器模式的设计是为了能够让对象的创建更加方便,一般是会设计两个类,一个类是目标类,也就是我们需要的类;另一个类就是构建器类,它是为了我们创建目标类时更加的方便。

2.构建器模式的使用场景
  1. 目标类的属性过多。当我们需要设置参数时,就需要一个一个去set,或者重载构造方法,前者就会让编写的代码十分繁琐;后者就难以重载自己想要的构造方法。
  2. 目标类的属性结构复杂。如果我们每次使用目标类时,都要手动的去设置属性,还要关心它们的内部结构,那么使用目标类的体验感是很差的。
3.优点
  1. 扩展性高
  2. 使用方便
4.缺点
  1. 需要创建构建器类
  2. 依赖性高。一旦目标类改变,构建器也需要改变。
5.原则

“+”代表遵守,“-”代表不遵守或者不相关

原则开放封闭单一职责迪米特里氏替换依赖倒置接口隔离合成复用-+-----
二、简单的构建器模式实现
1.代码

User

package build;

/**
 * @author xxj
 * 用户
 */
public class User {
    private String name;
    private int age;
    private String sex;
    public User() {
    }
    public User(UserBuild build) {
        this.age=build.age;
        this.name=build.name;
        this.sex=build.sex;
    }
     @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}
复制代码

UserBuild

package build;

/**
 * @author xxj
 * 用户构建器
 */
public class UserBuild {
    public String name;
    public int age;
    public String sex;
    public UserBuild(){}
    public UserBuild name(String name){
        this.name=name;
        return this;
    }
    public UserBuild age(int age){
        this.age=age;
        return this;
    }
    public UserBuild sex(String sex){
        this.sex=sex;
        return this;
    }
    public User build(){
        return new User(this);
    }
}
复制代码
2.使用
package test;

import build.User;
import build.UserBuild;

public class test {
    public static void main(String[] args){
        UserBuild build=new UserBuild();
        build.name("xiaoming").age(18).sex("男");
        User user=new User(build);
        System.out.println(user.toString());
    }
}
复制代码

结果 在这里插入图片描述

3.注意
  1. 构建器的创建时,需要注意,它的方法返回的是 this
  2. 构建器还可以给属性设置默认值,这样可以省去很多默认参数的设置。
4.最后

我发现这个构建器可以更加的简便一点,完全不需要创建多余的对象

/**
 * @author xuxiaobai
 * 用户
 */
public class User {
    private String name;
    private int age;
    private String sex;
    
    public User() {
       
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public User setAge(int age) {
        this.age = age;
        return this;
    }

    public String getSex() {
        return sex;
    }

    public User setSex(String sex) {
        this.sex = sex;
        return this;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}
复制代码

这里的每个 set 方法都会返回当前的对象,这样就省去了 UserBuild 这个类,想要使用也很简单,IDEA 就可以帮我们快速地生产模板。

IDEA 的 Builder 使用步骤:

  1. 按住 Alt + Insert ,或者按右键

image-20210623114654692

  1. 进入 Getter and Setter

image-20210623114729260

  1. 更改 Set 方法的模板

image-20210623114840842

  1. 最后选中全部属性,点击创建即可。
三、总结

构建器模式的设计思想就是能够更加便捷地创建一个对象,从上面的例子也可以看出,当属性较少时,没有必要创建一个构建器,但是我举这个例子想表达就是这个想法:创建一个构建器来构建一个对象。 当属性越来越复杂时,构建器的作用就会体现出来了。