其他网址
【狂神说Java】通俗易懂的23种设计模式教学(停更)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili
有序的建造者模式
简介
场景
假设的场景:建一座房子。需要如下步骤:打地基、建墙体、铺电线、粉刷。这些步骤的顺序是不可颠倒的,而且任意一步不可缺少。
所有的类
代码
房屋产品
package org.example.builder.product; public class House { private String base; private String wall; private String wire; private String paint; public String getBase() { return base; } public void setBase(String base) { this.base = base; } public String getWall() { return wall; } public void setWall(String wall) { this.wall = wall; } public String getWire() { return wire; } public void setWire(String wire) { this.wire = wire; } public String getPaint() { return paint; } public void setPaint(String paint) { this.paint = paint; } @Override public String toString() { return "House{" + "base='" + base + '/'' + ", wall='" + wall + '/'' + ", wire='" + wire + '/'' + ", paint='" + paint + '/'' + '}'; } }
建造者
抽象类
package org.example.builder.product; public abstract class Builder { public abstract Builder buildBase(String base); public abstract Builder buildWall(String wall); public abstract Builder buildWire(String wire); public abstract Builder buildPaint(String paint); public abstract House build(); }
实现类
重点:在构造函数中,创建一个产品。
package org.example.builder.product; public class Woker extends Builder { private House house; public Woker() { this.house = new House(); } @Override public Builder buildBase(String base) { System.out.println("建造地基"); house.setBase(base); return this; } @Override public Builder buildWall(String wall) { System.out.println("建造墙壁"); house.setWall(wall); return this; } @Override public Builder buildWire(String wire) { System.out.println("铺设电线"); house.setWire(wire); return this; } @Override public Builder buildPaint(String paint) { System.out.println("粉刷"); house.setPaint(paint); return this; } @Override public House build() { return this.house; } }
指挥者
简介
指挥者决定建造房子的顺序
指挥者决定具体每一步做什么样的建造。这样,可以造出简约、有大电流的电线、有隔音墙、地基为平房地基的平房,也可以造出其他配置的房子,比如:豪华粉刷、大电流、隔音墙、地基为高楼地基的高楼等。
代码
package org.example.builder; import org.example.builder.product.Builder; import org.example.builder.product.House; public class Director { public static House build(Builder builder) { return builder .buildBase("平房地基") .buildWall("隔音墙") .buildWire("大电流的电线") .buildPaint("简约粉刷") .build(); } }
测试类
简介
指挥者(包工头)去指挥具体的工人去按照自己的步骤建造房子。
代码
package org.example.builder; import org.example.builder.product.House; import org.example.builder.product.Woker; public class Consumer { public static void main(String[] args) { House build = Director.build(new Woker()); System.out.println(build); } }
执行结果
建造地基 建造墙壁 铺设电线 粉刷 House{base='平房地基', wall='隔音墙', wire='大电流的电线', paint='简约粉刷'}
无序的建造者模式
简介
场景
假设的场景:你去买一款手机,这款手机有多种可选的配置:颜色、运行内存、机身存储。
- 如你果不指定,则给一个默认项,比如:黑色,6G,128G。
- 如果你指定某些项,则给你你指定的配置,其他没指定的依然采用默认的配置。
所有的类
对比有序的建造者模式
无序,且每个字段有默认项,由用户来直接设置。
代码
手机产品
重点:提供静态的创建builder的方法;字段要设置默认值。
package org.example.builder.product; public class Phone { private String color = "黑色(默认)"; private String runningStorage = "6G(默认)"; private String phoneStorage = "128G(默认)"; public static DefaultBuilder builder() { return new DefaultBuilder(); } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getRunningStorage() { return runningStorage; } public void setRunningStorage(String runningStorage) { this.runningStorage = runningStorage; } public String getPhoneStorage() { return phoneStorage; } public void setPhoneStorage(String phoneStorage) { this.phoneStorage = phoneStorage; } @Override public String toString() { return "Phone{" + "color='" + color + '/'' + ", runningStorage='" + runningStorage + '/'' + ", phoneStorage='" + phoneStorage + '/'' + '}'; } }
建造者
建造者抽象类
package org.example.builder.product; public abstract class Builder { public abstract Builder buildColor(String color); public abstract Builder buildRunningStorage(String runningStorage); public abstract Builder buildPhoneStorage(String phoneStorage); public abstract Phone build(); }
建造者实现类
重点:构造函数里边,要去构造一个产品。
package org.example.builder.product; public class DefaultBuilder extends Builder { private Phone phone; public DefaultBuilder() { this.phone = new Phone(); } @Override public Builder buildColor(String color) { phone.setColor(color); return this; } @Override public Builder buildRunningStorage(String runningStorage) { phone.setRunningStorage(runningStorage); return this; } @Override public Builder buildPhoneStorage(String phoneStorage) { phone.setPhoneStorage(phoneStorage); return this; } @Override public Phone build() { return phone; } }
测试类
package org.example.builder; import org.example.builder.product.Phone; public class Consumer { public static void main(String[] args) { Phone phone = Phone.builder() .buildColor("红色") .buildRunningStorage("8G") .build(); System.out.println(phone); } }
执行结果
Phone{color='红色', runningStorage='8G', phoneStorage='128G(默认)'}
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/151030.html