介绍:
构造者模式,又称之为建造者模式,建造者模式,单例模式以及工厂模式都属于创建型模式
1
应用场景
今天学mybatis的时候,知道了SQLSessionFactory使用的是builder模式来生成的。再次整理一下什么是builder模式以及应用场景。
当一个bean类重载了多个构造方法时,并且参数随机使用时,考虑使用构造者模式,
builder模式也叫建造者模式,builder模式的作用将一个复杂对象的构建与他的表示分离,使用者可以一步一步的构建一个比较复杂的对象。
2.3 builder模式
package com.wangjun.designPattern.builder; public class Product3 { private final int id; private final String name; private final int type; private final float price; private Product3(Builder builder) { this.id = builder.id; this.name = builder.name; this.type = builder.type; this.price = builder.price; } public static class Builder { private int id; private String name; private int type; private float price; public Builder id(int id) { this.id = id; return this; } public Builder name(String name) { this.name = name; return this; } public Builder type(int type) { this.type = type; return this; } public Builder price(float price) { this.price = price; return this; } public Product3 build() { return new Product3(this); } } }
可以看到builder模式将属性定义为不可变的,然后定义一个内部静态类Builder来构建属性,再通过一个只有Builder参数的构造器来生成Product对象。Builder的setter方法返回builder本身,以便可以将属性连接起来。我们就可以像下面这样使用了。
Product3 p3 = new Product3.Builder() .id(10) .name("phone") .price(100) .type(1) .build();
当然具体使用builder的情况肯定没有这么简单,但是思路大致一样:先通过某种方式取得构造对象需要的所有参数,再通过这些参数一次性构建这个对象。比如MyBatis中SqlSessionFactoryBuilder就是通过读取MyBatis的xml配置文件来获取构造SqlSessionFactory所需要的参数的。
1
Demo
package com.yunsuibi; public class User { private final String firstName; // required private final String lastName; // required private final int age; // optional private final String phone; // optional private final String address; // optional private User(UserBuilder builder) { this.firstName = builder.firstName; this.lastName = builder.lastName; this.age = builder.age; this.phone = builder.phone; this.address = builder.address; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } public String getPhone() { return phone; } public String getAddress() { return address; } @Override public String toString() { return "User [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", phone=" + phone + ", address=" + address + "]"; } public static class UserBuilder { private final String firstName; private final String lastName; private int age; private String phone; private String address; public UserBuilder(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public UserBuilder age(int age) { this.age = age; return this; } public UserBuilder phone(String phone) { this.phone = phone; return this; } public UserBuilder address(String address) { this.address = address; return this; } public User build() { return new User(this); } } }
运行
public static void main(String[] args) { User build = new User.UserBuilder("Jhon", "Doe").address("北京").build(); System.out.println(build); User build2 = new User.UserBuilder("李四", "哈哈").address("12").build(); System.out.println(build2);
}
参考:java之构造者模式