抽象的建造者
/**
* @program: DesignPattern
* @author: Yao
* @create: 2020-05-05 11:36
*
* 抽象的建造者
**/
public abstract class Builder {
abstract void buildA(); // 地基
abstract void buildB(); // 钢筋工程
abstract void buildC(); // 铺电线
abstract void buildD(); // 粉刷
// 完工,得到产品
abstract Product getProduct();
}
具体的建造者:工人
/**
* @program: DesignPattern
* @author: Yao
* @create: 2020-05-05 11:38
* 具体的建造者:工人
**/
public class Worker extends Builder {
private Product product;
public Worker() {
product = new Product();
}
@Override
void buildA() {
product.setBuildA("地基");
System.out.println("地基");
}
@Override
void buildB() {
product.setBuildB("钢筋工程");
System.out.println("钢筋工程");
}
@Override
void buildC() {
product.setBuildC("铺电线");
System.out.println("铺电线");
}
@Override
void buildD() {
product.setBuildD("粉刷");
System.out.println("粉刷");
}
@Override
Product getProduct() {
return product;
}
}
产品
public class Product {
private String buildA;
private String buildB;
private String buildC;
private String buildD;
@Override
public String toString() {
return "Product{" +
"buildA='" + buildA + '\'' +
", buildB='" + buildB + '\'' +
", buildC='" + buildC + '\'' +
", buildD='" + buildD + '\'' +
'}';
}
public String getBuildA() {
return buildA;
}
public void setBuildA(String buildA) {
this.buildA = buildA;
}
public String getBuildB() {
return buildB;
}
public void setBuildB(String buildB) {
this.buildB = buildB;
}
public String getBuildC() {
return buildC;
}
public void setBuildC(String buildC) {
this.buildC = buildC;
}
public String getBuildD() {
return buildD;
}
public void setBuildD(String buildD) {
this.buildD = buildD;
}
}
指挥:核心。负责指挥构建一个工程,工程如何构建,由他决定
/**
* @program: DesignPattern
* @author: Yao
* @create: 2020-05-05 11:43
*
*
* 指挥:核心。负责指挥构建一个工程,工程如何构建,由他决定
**/
public class Director {
// 指挥工人按照顺序建房子
public Product build(Builder builder){
builder.buildC();
builder.buildA();
builder.buildB();
builder.buildD();
return builder.getProduct();
}
}
测试
public class Test {
public static void main(String[] args) {
// 指挥
Director director = new Director();
// 指挥 具体的工人完成产品
Product build = director.build(new Worker());
System.out.println(build);
}
}
抽象建造者
public abstract class Builder {
abstract Builder buildA(String msg); // 汉堡
abstract Builder buildB(String msg); // 可乐
abstract Builder buildC(String msg); // 薯条
abstract Builder buildD(String msg); // 甜点
abstract Product getProduct();
}
具体建造者:服务员
/**
* @program: DesignPattern
* @author: Yao
* @create: 2020-05-05 11:56
*
* 具体的建造者
**/
public class Worker extends Builder {
private Product product;
public Worker() {
product = new Product();
}
@Override
Builder buildA(String msg) {
product.setBuildA(msg);
return this;
}
@Override
Builder buildB(String msg) {
product.setBuildB(msg);
return this;
}
@Override
Builder buildC(String msg) {
product.setBuildC(msg);
return this;
}
@Override
Builder buildD(String msg) {
product.setBuildD(msg);
return this;
}
@Override
Product getProduct() {
return product;
}
}
产品:套餐,有默认的内容
/**
* @program: DesignPattern
* @author: Yao
* @create: 2020-05-05 11:54
*
* 产品:套餐
**/
public class Product {
private String BuildA = "汉堡";
private String BuildB = "可乐";
private String BuildC = "薯条";
private String BuildD = "甜点";
@Override
public String toString() {
return "Product{" +
"BuildA='" + BuildA + '\'' +
", BuildB='" + BuildB + '\'' +
", BuildC='" + BuildC + '\'' +
", BuildD='" + BuildD + '\'' +
'}';
}
public String getBuildA() {
return BuildA;
}
public void setBuildA(String buildA) {
BuildA = buildA;
}
public String getBuildB() {
return BuildB;
}
public void setBuildB(String buildB) {
BuildB = buildB;
}
public String getBuildC() {
return BuildC;
}
public void setBuildC(String buildC) {
BuildC = buildC;
}
public String getBuildD() {
return BuildD;
}
public void setBuildD(String buildD) {
BuildD = buildD;
}
}
测试
public class Test {
public static void main(String[] args) {
// 服务员
Worker worker = new Worker();
// 链式编程 : 在原来的基础上,可以自由组合了,如果不组合,也有默认的套餐
Product product = worker.buildA("全家桶").buildB("雪碧").getProduct();
System.out.println(product);
}
}
抽象建造者:
public abstract class Builder {
abstract Builder assembleCPU(String msg);
abstract Builder assembleRAM(String msg);
abstract Builder assembleHardDrive(String msg);
abstract Builder assembleDisplayer(String msg);
abstract Product getProduct();
}
具体的建造者:这里是组装电脑的工作人员
public class Assembler extends Builder{
private Product product;
public Assembler() {
product = new Product();
}
@Override
Builder assembleCPU(String msg) {
product.setCPU(msg);
return this;
}
@Override
Builder assembleRAM(String msg) {
product.setRAM(msg);
return this;
}
@Override
Builder assembleHardDrive(String msg) {
product.setHardDrive(msg);
return this;
}
@Override
Builder assembleDisplayer(String msg) {
product.setDisplayer(msg);
return this;
}
@Override
Product getProduct() {
return product;
}
}
产品:组成起来的电脑
@SuppressWarnings("all")
public class Product {
private String CPU = "i9 9900KF";
private String RAM = "G.SKILL Royal DDR4 3200 16G";
private String hardDrive = "SAMSUNG 970 EVO Plus 1t";
private String displayer = "(ASUS)ROG PG65UQ 144HZ 4K HDR";
@Override
public String toString() {
return "Product{" +
"CPU='" + CPU + '\'' + "\n" +
", RAM='" + RAM + '\'' + "\n" +
", hardDrive='" + hardDrive + '\'' + "\n" +
", displayer='" + displayer + '\'' + "\n" +
'}';
}
public String getCPU() {
return CPU;
}
public void setCPU(String CPU) {
this.CPU = CPU;
}
public String getRAM() {
return RAM;
}
public void setRAM(String RAM) {
this.RAM = RAM;
}
public String getHardDrive() {
return hardDrive;
}
public void setHardDrive(String hardDrive) {
this.hardDrive = hardDrive;
}
public String getDisplayer() {
return displayer;
}
public void setDisplayer(String displayer) {
this.displayer = displayer;
}
}
test:可随意调配电脑的组件
public class Test {
public static void main(String[] args) {
// 装电脑 的工作人员
Assembler assembler = new Assembler();
Product product = assembler.assembleDisplayer("一个垃圾显示器就好了").getProduct();
System.out.println(product);
}
}
UML图: