Java創建型模式之建造者模式詳解
一.介紹
建造者模式(Builder Pattern)屬於創建型模式。如果一個對象具有復雜的內部結構或者內部屬性本身相互依賴(有順序要求),甚至對象中的某些屬性的創建也有一個很復雜的過程,就可以使用建造者模式
二.UML類圖
三.具體代碼
業務代碼
public class Product { private String part1; private String part2; public Product(String part1, String part2) { this.part1 = part1; this.part2 = part2; } @Override public String toString() { return "Product{" + "part1='" + part1 + '\'' + ", part2='" + part2 + '\'' + '}'; } } //控制每個部件的具體創建 interface Builder { void buildPart1(String part1); void buildPart2(String part2); Product build(); } class ChinaBuilder implements Builder{ private String part1; private String part2; @Override public void buildPart1(String part1) { this.part1 = "國產" + part1; } @Override public void buildPart2(String part2) { this.part2 = "國產" + part2; } @Override public Product build() { return new Product(part1, part2); } } //控制組裝部件的順序 class Director{ private Builder builder; public Director(Builder builder) { this.builder = builder; } public Product construct(String part1, String part2){ builder.buildPart1(part1); builder.buildPart2(part2); return builder.build(); } }
測試代碼
public class BuilderTest { public static void main(String[] args) { Director director = new Director(new ChinaBuilder()); Product product = director.construct("傳感器", "芯片"); System.out.println(product); //Product{part1='國產傳感器', part2='國產芯片'} } }
四.lombok的@Builder註解(拓展)
我們先以靜態內部類的方式實現簡單場景下的建造者模式(以優雅的鏈式調用代替set)
業務代碼
public class Product { private String part1; private String part2; public Product(String part1, String part2) { this.part1 = part1; this.part2 = part2; } @Override public String toString() { return "Product{" + "part1='" + part1 + '\'' + ", part2='" + part2 + '\'' + '}'; } static class Builder { private String part1; private String part2; public Builder part1(String part1) { this.part1 = part1; return this; } public Builder part2(String part2) { this.part2 = part2; return this; } public Product build(){ return new Product(part1, part2); } } }
測試類
public class ProductTest { public static void main(String[] args) { Product product = new Product.Builder().part1("傳感器").part2("芯片").build(); System.out.println(product); //Product{part1='傳感器', part2='芯片'} } }
下面看看lombok的@Builder註解的實現
業務代碼
@Builder public class Product { private String part1; private String part2; }
在IDEA中打開Product.class文件,可以看出實際就是使用的簡單場景的建造者模式
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package com.designpattern.create.builder.lombok; public class Product { private String part1; private String part2; Product(String part1, String part2) { this.part1 = part1; this.part2 = part2; } public static Product.ProductBuilder builder() { return new Product.ProductBuilder(); } public static class ProductBuilder { private String part1; private String part2; ProductBuilder() { } public Product.ProductBuilder part1(String part1) { this.part1 = part1; return this; } public Product.ProductBuilder part2(String part2) { this.part2 = part2; return this; } public Product build() { return new Product(this.part1, this.part2); } public String toString() { return "Product.ProductBuilder(part1=" + this.part1 + ", part2=" + this.part2 + ")"; } } }
五.在Spring中的應用
org.springframework.web.servlet.mvc.method.RequestMappingInfo.DefaultBuilder
到此這篇關於Java創建型模式之建造者模式詳解的文章就介紹到這瞭,更多相關Java建造者模式內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 深入理解Java設計模式之建造者模式
- Java設計模式之構建者模式知識總結
- Java建造者模式構建復雜對象的最佳實踐
- Java中常用的設計模式之建造者模式詳解
- Java中lombok的@Builder註解的解析與簡單使用詳解