建造者模式将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式是对一个个体的创建过程进行细分,拆解为不同的创建部分 。
注:上面图片来源于https://mp.weixin.qq.com/s/yR0Fceqsz0dJqOYTpzq-yw
import lombok.Builder;
/**
* @author xch
* 2023/2/2 15:45
*/
@Builder
public class Product {
private String partA;
private String partB;
private String partC;
public static void main(String[] args) {
Product build = Product.builder().partA("A").partB("B").partC("C").build();
System.out.println(build);
}
}
打包后,再使用idea查看源码:可看到@Bulider这个注解做了什么:
import java.beans.ConstructorProperties;
public class Product {
private String partA;
private String partB;
private String partC;
@ConstructorProperties({"partA", "partB", "partC"})
Product(String partA, String partB, String partC) {
this.partA = partA;
this.partB = partB;
this.partC = partC;
}
public static Product.ProductBuilder builder() {
return new Product.ProductBuilder();
}
public static class ProductBuilder {
private String partA;
private String partB;
private String partC;
ProductBuilder() {
}
public Product.ProductBuilder partA(String partA) {
this.partA = partA;
return this;
}
public Product.ProductBuilder partB(String partB) {
this.partB = partB;
return this;
}
public Product.ProductBuilder partC(String partC) {
this.partC = partC;
return this;
}
public Product build() {
return new Product(this.partA, this.partB, this.partC);
}
public String toString() {
return "Product.ProductBuilder(partA=" + this.partA + ", partB=" + this.partB + ", partC=" + this.partC + ")";
}
}
}
/**
* 电脑产品
* @author xch
* 2022/1/19 18:44
*/
public class Computer {
private String cpu;
private String brandName;
private String mainBoard;
private String usb;
private String color;
public void setUsb(String usb) {
this.usb = usb;
}
public void setColor(String color) {
this.color = color;
}
public Computer(String cpu, String brandName, String mainBoard) {
this.cpu = cpu;
this.brandName = brandName;
this.mainBoard = mainBoard;
}
@Override
public String toString() {
return "Computer{" +
"cpu='" + cpu + '\'' +
", brandName='" + brandName + '\'' +
", mainBoard='" + mainBoard + '\'' +
", usb='" + usb + '\'' +
", color='" + color + '\'' +
'}';
}
}
/**
* 电脑构建工具
* @author xch
* 2022/1/19 18:42
*/
public abstract class ComputerBuilder {
abstract void usb();
abstract void color();
abstract Computer getResult();
}
/**
* 华为品牌的电脑构建工具
* @author xch
* 2022/1/19 18:55
*/
public class HuaweiComputerBuilder extends ComputerBuilder{
private Computer computer;
public HuaweiComputerBuilder() {
computer = new Computer("i7-3000", "Huawei-book", "微星200");
}
@Override
void usb() {
computer.setUsb("空");
}
@Override
void color() {
computer.setColor("黑色");
}
@Override
public Computer getResult() {
return computer;
}
}
/**
* mac品牌的电脑构建工具
* @author xch
* 2022/1/19 18:47
*/
public class MacComputerBuilder extends ComputerBuilder{
private Computer computer;
public MacComputerBuilder() {
computer = new Computer("i5-8000","Mac-Ipad", "迫击炮B250");
}
@Override
void usb() {
computer.setUsb("触摸");
}
@Override
void color() {
computer.setColor("银色");
}
@Override
public Computer getResult() {
return computer;
}
}
/**
* 电脑建造者,控制构建什么品牌的电脑
* @author xch
* 2022/1/19 18:57
*/
public class ComputerDirector {
private ComputerBuilder builder;
public ComputerDirector(ComputerBuilder builder) {
this.builder = builder;
}
public Computer makeComputer() {
builder.color();
builder.usb();
return builder.getResult();
}
}
public class Main {
public static void main(String[] args) {
ComputerBuilder builder = new HuaweiComputerBuilder();
final ComputerDirector computerDirector = new ComputerDirector(builder);
final Computer computer = computerDirector.makeComputer();
System.out.println(computer);
ComputerBuilder builder1 = new MacComputerBuilder();
final ComputerDirector computerDirector1 = new ComputerDirector(builder1);
final Computer computer1 = computerDirector1.makeComputer();
System.out.println(computer1);
}
}
1.符合面向对象的封装原则,构成过程和最终表示完全分离。
2.避免了代码臃肿,冗余,出错几率,使用了建造者模式,在很大程度上减少了系统的耦合,因为实现细节都已经屏蔽,所以开发者不需要去自己构建。
1.如果构建的对象发生了大的变化,那么我们需要去修改内部代码,这样就增加一定的成本。
2.构建的对象是固定的,各组成成分都需要按照要求来进行,所以灵活度不高。
/**
* Builder for {@link UriComponents}.
*
* Typical usage involves:
*
* - Create a {@code UriComponentsBuilder} with one of the static factory methods
* (such as {@link #fromPath(String)} or {@link #fromUri(URI)})
* - Set the various URI components through the respective methods ({@link #scheme(String)},
* {@link #userInfo(String)}, {@link #host(String)}, {@link #port(int)}, {@link #path(String)},
* {@link #pathSegment(String...)}, {@link #queryParam(String, Object...)}, and
* {@link #fragment(String)}.
* - Build the {@link UriComponents} instance with the {@link #build()} method.
*
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @author Phillip Webb
* @author Oliver Gierke
* @author Brian Clozel
* @author Sebastien Deleuze
* @since 3.1
* @see #newInstance()
* @see #fromPath(String)
* @see #fromUri(URI)
*/
public class UriComponentsBuilder implements UriBuilder, Cloneable {
...
public static UriComponentsBuilder fromUriString(String uri) {...}
public static UriComponentsBuilder fromHttpUrl(String httpUrl) {...}
public static UriComponentsBuilder fromHttpRequest(HttpRequest request) {...}
...
}