Java设计模式详解
参考:
https://www.runoob.com/design-pattern/design-pattern-tutorial.html
建造者模式:
场景1:
(1)构造一个复杂的对象,包含很多的属性,有些属性构造的时候需要做一些校验,格式转换等可能各个部分经常面临着剧烈的变化。
(2)比如:一个对象里面有很多属性:field1,field2,field3,... 我们需要构造这个大对象。
场景2:
(1) 电商项目中,商品中心的商品,采购中心的采购单,仓储中心的:采购入库单,销售出库单,退货入库单;订单中心的订单 等都比较复杂,可以使用构造器模式
demo1:
package test.builderTest; public class Test { public static void main(String[] args) { Cbuild cbuild = new Cbuild(); Direct direct = new Direct(cbuild); Product product = direct.biluder("value1", "value2", "value3"); System.out.println(product); } public static class Direct { private Biluder biluder; public Direct (Biluder biluder) { this.biluder=biluder; } Product biluder (String field1,String field2,String field3){ biluder.set1(field1); biluder.set2(field2); biluder.set3(field3); return biluder.create(); } } public interface Biluder { void set1(String s1); void set2(String s2); void set3(String s3); Product create(); } public static class Cbuild implements Biluder{ private Product product = new Product(); @Override public void set1(String s1) { product.setS1(s1); } @Override public void set2(String s2) { product.setS2(s2); } @Override public void set3(String s3) { product.setS3(s3); } @Override public Product create() { return product; } } public static class Product { String s1; String s2; String s3; public String getS1() { return s1; } public void setS1(String s1) { this.s1 = s1; } public String getS2() { return s2; } public void setS2(String s2) { this.s2 = s2; } public String getS3() { return s3; } public void setS3(String s3) { this.s3 = s3; } @Override public String toString() { return "Product{" + "s1='" + s1 + '\'' + ", s2='" + s2 + '\'' + ", s3='" + s3 + '\'' + '}'; } } }
demo2:
package test.builderTest; public class OptimizedBuilderPattern2 { public static void main(String[] args) { Builder builder = new ConcreteBuilder(); Product product = builder .field1("value1") .field2("value2") .field3("value3") .create(); System.out.println(product); } public interface Builder{ Builder field1(String field1) ; Builder field2(String field2) ; Builder field3(String field3) ; Product create(); } public static class ConcreteBuilder implements Builder{ private Product product = new Product() ; public Builder field1(String field1) { product.setField1(field1); return this ; } @Override public Builder field2(String field2) { product.setField2(field2); return this; } @Override public Builder field3(String field3) { product.setField3(field3); return this ; } @Override public Product create() { return product; } } //==================商品类============ public static class Product{ private String field1; private String field2; private String field3; public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } public String getField2() { return field2; } public void setField2(String field2) { this.field2 = field2; } public String getField3() { return field3; } public void setField3(String field3) { this.field3 = field3; } @Override public String toString() { return "Product{" + "field1='" + field1 + '\'' + ", field2='" + field2 + '\'' + ", field3='" + field3 + '\'' + '}'; } } }
ps:通过接口对象,可以使用实现类的方法进行赋值
3.2 使用一般builder模式
- 好处1:通过builder接口将复杂构建步骤拆分成了多个部分,代码逻辑清晰,维护性和扩展性都很好
- 好处2:将对象构建的过程,封装在了director里面,director来基于builder进行构建,构建逻辑修改,不需要修改很多地方
- 好处3:相对于工厂,有一个很好的抽象设计,director和builder
3.3 使用一般builder模式
- 这种模式下去除了Director,每次构建完一个属性之后会返回builder.基于Builder最后返回目标对象。
设计模式之构造器模式(9):封装复杂对象的构造逻辑
posted on 2021-09-07 14:20 RICH-ATONE 阅读(106) 评论(0) 编辑 收藏 举报