开发者

Java创建型模式之建造者模式详解

开发者 https://www.devze.com 2023-02-18 10:39 出处:网络 作者: 非凡的小笨鱼
目录一.介绍二.UML类图三.具体代码四.lombok的@Builder注解(拓展)五.在Spring中的应用一.介绍
目录
  • 一.介绍
  • 二.UML类图
  • 三.具体代码
  • 四.lombok的@Builder注解(拓展)
  • 五.在Spring中的应用

一.介绍

建造者模式(Builder Pattern)属于创建型模式。如果一个对象具有复杂的内部结构或者内部属性本身相互依赖(有顺序要求),甚至对象中的某些属性的创建也有一个很复杂的过程,就可以使用建造者模式

二.UML类图

Java创建型模式之建造者模式详解

三.具体代码

业务代码

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 = pjsar编程客栈t2;
            return this;
        }
        public Product build(){
            return new Product(part1, part2);
        }
    }
}

测试类

public class ProductTesGlGxft {
    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.http://www.devze.comProductBuilder part2(String part2) {
            this.part2 = part2;
            return this;
        }
        public Product build() {
            return new Product(this.part1, this.part2);
        }
        public Strin开发者_C培训g toString() {
            return "Product.ProductBuilder(part1=" + this.part1 + ", part2=" + this.part2 + ")";
        }
    }
}

五.在Spring中的应用

org.springfra编程客栈mework.web.servlet.mvc.method.RequestMappingInfo.DefaultBuilder

到此这篇关于Java创建型模式之建造者模式详解的文章就介绍到这了,更多相关Java建造者模式内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号