这篇文章主要介绍了Java创建型模式之建造者模式怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Java创建型模式之建造者模式怎么实现文章都会有所收获,下面我们一起来看看吧。
建造者模式(Builder Pattern)属于创建型模式。如果一个对象具有复杂的内部结构或者内部属性本身相互依赖(有顺序要求),甚至对象中的某些属性的创建也有一个很复杂的过程,就可以使用建造者模式
业务代码
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='国产芯片'} } }
我们先以静态内部类的方式实现简单场景下的建造者模式(以优雅的链式调用代替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 + ")"; } } }
org.springframework.web.servlet.mvc.method.RequestMappingInfo.DefaultBuilder
关于“Java创建型模式之建造者模式怎么实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Java创建型模式之建造者模式怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。