【Java】23种设计模式——4.建造者模式

概念

当构造对象参数较多是,可以使用链式方法创建对象,保证参数填写正确。我们通过建造者不断配置参数和内容,保障参数填写正确。

建造者模式通常使用XXXBuiler类型。例如,StringBuilder的使用。

StringBuilder builder = new StringBuilder();    // 创建一个StringBuilder对象
builder.append("hello")     // 拼接字符串
		.append(" ")
		.append("world")
		.append(2025)     //拼接数字
		.insert(2,","); // 在第3个位置插入字符串
System.out.println(builder.toString());     // he, llo world2025

应用场景

  • 一份套餐(包含多个选项的复杂订单)
  • 一辆汽车(包含发动机、轮胎、底盘、车身等)
  • 电脑组装平台 (分步设置CPU、内存、硬盘等组件),同一个构建流程可生成游戏电脑(高性能配件)或办公电脑(基础配件)

使用

建造者方法

/**
 * 实体类编写 :学生
 * @Author:lyj
 * @Date:2025/5/5 11:24
 */
public class Student {
    int id;         // id
    int age;        // 年龄
    int grade;      // 年级
    String name;    // 姓名
    String college; // 大学
    String profession;  // 专业
    List<String> awards;    // 奖励

    /**
     * 私有化构造方法。不对外直接开发
     * @param id id
     * @param age 年龄
     * @param grade 年级
     * @param name 姓名
     * @param college 大学
     * @param profession 专业
     * @param awards 奖励
     */
    private Student(int id, int age, int grade, String name, String college, String profession, List<String> awards) {
        this.id = id;
        this.age = age;
        this.grade = grade;
        this.name = name;
        this.college = college;
        this.profession = profession;
        this.awards = awards;
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", age=" + age +
                ", grade=" + grade +
                ", name='" + name + '\'' +
                ", college='" + college + '\'' +
                ", profession='" + profession + '\'' +
                ", awards=" + awards +
                '}';
    }
    /**
     * 通过build创建建造者
     * @return
     */
    public static StudentBuilder builder() {
        return new StudentBuilder();
    }

    /**
     * 直接创建一个静态内部类。
     * 参数与Student一致
     */
    public static class StudentBuilder {
        int id;         // id
        int age;        // 年龄
        int grade;      // 年级
        String name;    // 姓名
        String college; // 大学
        String profession;  // 专业
        List<String> awards;    // 奖励

        /**
         * 设置id
         * @param id id
         * @return
         */
        public StudentBuilder id( int id) {
            this.id = id;
            return this;
        }
        /**
         * 设置年龄
         * @param age 年龄
         * @return
         */
        public StudentBuilder age(int age) {
            this.age = age;
            return this;
        }

        /**
         * 设置年级
         * @param grade 年级
         * @return
         */
        public StudentBuilder grade(int grade) {
            this.grade = grade;
            return this;
        }

        /**
         * 设置姓名
         * @param name 姓名
         * @return
         */
        public StudentBuilder name(String name) {
            this.name = name;
            return this;
        }

        /**
         * 设置大学
         * @param college 大学
         * @return
         */
        public StudentBuilder college(String college) {
            this.college = college;
            return this;
        }

        /**
         * 设置专业
         * @param profession 专业
         * @return
         */
        public StudentBuilder profession(String profession) {
            this.profession = profession;
            return this;
        }
        /**
         * 添加奖励
         * @param awards 奖励
         * @return
         */
        public StudentBuilder awards(List<String> awards) {
            this.awards = awards;
            return this;
        }

        /**
         * 构建对象
         * @return
         */
        public Student build() {
            return new Student(id, age, grade, name, college, profession, awards);
        }
    }
}

建造者模式使用:

Student student = Student.builder()
        .id(1)
        .college("计算机科学与技术")
        .profession("软件工程")
        .grade(2025)
        .awards(List.of(new String[]{"优秀", "优秀"}))
        .name("张三")
        .age(18)
        .build();
System.out.println(student);    // Student{id=1, age=18, grade=2025, name='张三', college='计算机科学与技术', profession='软件工程', awards=[优秀, 优秀]}
posted @ 2025-06-20 15:19  陆陆无为而治者  阅读(16)  评论(0)    收藏  举报