介紹:
構造者模式,又稱之為建造者模式,建造者模式,單例模式以及工廠模式都屬於創建型模式
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之構造者模式