首頁 > 軟體

Java建立型模式之建造者模式詳解

2023-02-20 06:00:32

一.介紹

建造者模式(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建造者模式內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com