构建器模式:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
构建者模式其实就是我们常用的构建器,lombok的Builder注解就是构建器模式,effective中也比较推荐这种方式,相比于构造器中容易误差参数,构造器参数更清晰。
通过构造函数创建对象
通过创建空对象然后set属性
自定义不同参数的构造函数
lombok提供的注解@Builder链式调用
使用场景
当一个类的构造函数参数个数超过4个,而且这些参数有些是可选的参数,考虑使用构造者模式。
/** * Car对象 * 通过lombok的@Builder方式创建 * * @author 20023262 * @date 2020/12/10 * @since 1.0 */ @Setter @Getter @ToString @Builder @NoArgsConstructor @AllArgsConstructor public class Car { private Integer number; private String name; private String brand; }
/** * Person对象 * 通过不同参数自定义构造函数方式创建对象 * * @author 20023262 * @date 2020/12/10 * @since 1.0 */ @Setter @Getter @ToString @NoArgsConstructor public class Person { //必要参数 private int id; private String name; //可选参数 private int age; private String sex; private String phone; private String address; private String desc; /** * 各个参数构造函数 */ public Person(int id, String name) { this(id, name, 0); } public Person(int id, String name, int age) { this(id, name, age, ""); } public Person(int id, String name, int age, String sex) { this(id, name, age, sex, ""); } public Person(int id, String name, int age, String sex, String phone) { this(id, name, age, sex, phone, ""); } public Person(int id, String name, int age, String sex, String phone, String address) { this(id, name, age, sex, phone, address, ""); } public Person(int id, String name, int age, String sex, String phone, String address, String desc) { this.id = id; this.name = name; this.age = age; this.sex = sex; this.phone = phone; this.address = address; this.desc = desc; } }
/** * 自定义构建者模式对象 * * @author 20023262 * @date 2020/12/10 * @since 1.0 */ @Getter @Setter @ToString @NoArgsConstructor @AllArgsConstructor public class PersonOne { //必要参数 private int id; private String name; //可选参数 private int age; private String sex; private String phone; private String address; private String desc; /** * 通过builder将值设置到PersonOne对象 */ private PersonOne(Builder builder) { this.id = builder.id; this.name = builder.name; this.age = builder.age; this.sex = builder.sex; this.phone = builder.phone; this.address = builder.address; this.desc = builder.desc; } public static class Builder { //必要参数 private final int id; private final String name; //可选参数 private int age; private String sex; private String phone; private String address; private String desc; /** * 设置必须参数 */ public Builder(int id, String name) { this.id = id; this.name = name; } /** * 以下设置可选参数 */ public Builder age(int val) { this.age = val; return this; } public Builder sex(String val) { this.sex = val; return this; } public Builder phone(String val) { this.phone = val; return this; } public Builder address(String val) { this.address = val; return this; } public Builder desc(String val) { this.desc = val; return this; } /** * 通过builder方法创建PersonOne对象 */ public PersonOne build() { return new PersonOne(this); } } }
/** * 构建者模式 * * @author 20023262 * @date 2020/12/10 * @since 1.0 */ public class BuilderTest { /** * 方式一 * 通过lombok的Builder方式链式创建对象 */ @Test public void builderCarTest() { Car car = Car.builder().number(1001).name("帕梅拉").brand("Porsche").build(); } /** * 构建对象 */ @Test public void builderPersonTest() { /** * 通过创建空对象,依次set属性 */ Person person = new Person(); person.setId(1); person.setName("王小二"); person.setAge(18); person.setSex("男"); person.setPhone("15765481256"); person.setAddress("成都"); person.setDesc("产品"); /** * 通过构造函数 */ Person person1 = new Person(3, "张三"); Person person2 = new Person(3, "李四", 32); Person person3 = new Person(1, "赵柳", 21, "女", "123123", "sc", "ceshi"); /** * 自定义Builder */ PersonOne personOne = new PersonOne.Builder(1, "大疆").age(19).sex("nan").phone("123").address("bj").desc("air").build(); System.out.println("自定义Builder创建PersonOne:"+personOne); } }