介紹:
構造者模式,又稱之爲建造者模式,建造者模式,單例模式以及工廠模式都屬於建立型模式
1
應用場景html
今天學mybatis的時候,知道了SQLSessionFactory使用的是builder模式來生成的。再次整理一下什麼是builder模式以及應用場景。
當一個bean類重載了多個構造方法時,而且參數隨機使用時,考慮使用構造者模式,java
builder模式也叫建造者模式,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所須要的參數的。mybatis
1
Demoide
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); } } }
運行post
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之構造者模式ui
參考:Java設計模式之builder模式this