diff --git a/src/main/java/com/pnkx/designpatterns/builder/BYDBuilder.java b/src/main/java/com/pnkx/designpatterns/builder/BYDBuilder.java index b7c9190..8f2ebe5 100644 --- a/src/main/java/com/pnkx/designpatterns/builder/BYDBuilder.java +++ b/src/main/java/com/pnkx/designpatterns/builder/BYDBuilder.java @@ -6,7 +6,27 @@ package com.pnkx.designpatterns.builder; * @author 裴浩宇 * @version 1.0 * @date 2023/10/27 15:15 - * @description BYD汽车建造 + * @description BYD汽车建造类 */ -public class BYDBuilder { +public class BYDBuilder extends Builder { + @Override + public void setEngine() { + System.out.println("开始组装比亚迪发动机"); + car.setEngine("比亚迪发动机"); + System.out.println("比亚迪发动机组装完成"); + } + + @Override + public void setTire() { + System.out.println("开始组装比亚迪轮胎"); + car.setTire("比亚迪轮胎"); + System.out.println("比亚迪轮胎组装完成"); + } + + @Override + public void setSeat() { + System.out.println("开始组装比亚迪座位"); + car.setSeat("比亚迪座位"); + System.out.println("比亚迪座位组装完成"); + } } diff --git a/src/main/java/com/pnkx/designpatterns/builder/Builder.java b/src/main/java/com/pnkx/designpatterns/builder/Builder.java index 25ca02c..2605126 100644 --- a/src/main/java/com/pnkx/designpatterns/builder/Builder.java +++ b/src/main/java/com/pnkx/designpatterns/builder/Builder.java @@ -14,25 +14,25 @@ public abstract class Builder { protected Car car = new Car(); /** - * 建造发动机 + * 组装发动机 */ - public abstract void buildEngine(); + public abstract void setEngine(); /** - * 建造轮胎 + * 组装轮胎 */ - public abstract void buildTire(); + public abstract void setTire(); /** - * 建造座位 + * 组装座位 */ - public abstract void buildSeat(); + public abstract void setSeat(); /** - * 获取车 + * 输出车 * @return 车 */ - public Car getCar() { + public Car outputCar() { return car; } } diff --git a/src/main/java/com/pnkx/designpatterns/builder/Car.java b/src/main/java/com/pnkx/designpatterns/builder/Car.java index 98cb5e6..2a6ebd9 100644 --- a/src/main/java/com/pnkx/designpatterns/builder/Car.java +++ b/src/main/java/com/pnkx/designpatterns/builder/Car.java @@ -40,4 +40,13 @@ public class Car { public void setSeat(String seat) { this.seat = seat; } + + @Override + public String toString() { + return "Car{" + + "engine='" + engine + '\'' + + ", tire='" + tire + '\'' + + ", seat='" + seat + '\'' + + '}'; + } } diff --git a/src/main/java/com/pnkx/designpatterns/builder/Director.java b/src/main/java/com/pnkx/designpatterns/builder/Director.java new file mode 100644 index 0000000..ddce5a9 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/builder/Director.java @@ -0,0 +1,34 @@ +package com.pnkx.designpatterns.builder; + +/** + * Director + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/29 9:29 + * @description 指导者类 + */ +public class Director { + + // 建造者 + private final Builder builder; + + /** + * 构造方法 + * @param builder 建造者 + */ + public Director(Builder builder) { + this.builder = builder; + } + + /** + * 开始造车 + * @return 车 + */ + public Car buildCar() { + builder.setEngine(); + builder.setTire(); + builder.setSeat(); + return builder.outputCar(); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/builder/Main.java b/src/main/java/com/pnkx/designpatterns/builder/Main.java new file mode 100644 index 0000000..a0779ee --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/builder/Main.java @@ -0,0 +1,24 @@ +package com.pnkx.designpatterns.builder; + +/** + * Main + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/29 9:33 + * @description 客户端 + */ +public class Main { + public static void main(String[] args) { + // 初始化比亚迪汽车指导类 + Director BYDDirector = new Director(new BYDBuilder()); + // 开始制造比亚迪汽车 + Car BYDcar = BYDDirector.buildCar(); + System.out.println(BYDcar.toString()); + // 初始化欧尚汽车指导类 + Director OSHANDirector = new Director(new OSHANBuilder()); + // 开始制造欧尚汽车 + Car OSHANCar = OSHANDirector.buildCar(); + System.out.println(OSHANCar.toString()); + } +} diff --git a/src/main/java/com/pnkx/designpatterns/builder/OSHANBuilder.java b/src/main/java/com/pnkx/designpatterns/builder/OSHANBuilder.java new file mode 100644 index 0000000..16700f9 --- /dev/null +++ b/src/main/java/com/pnkx/designpatterns/builder/OSHANBuilder.java @@ -0,0 +1,32 @@ +package com.pnkx.designpatterns.builder; + +/** + * OSHANBuilder + * + * @author 裴浩宇 + * @version 1.0 + * @date 2023/10/29 9:24 + * @description 欧尚汽车建造类 + */ +public class OSHANBuilder extends Builder { + @Override + public void setEngine() { + System.out.println("开始组装欧尚发动机"); + car.setEngine("欧尚发动机"); + System.out.println("欧尚发动机组装完成"); + } + + @Override + public void setTire() { + System.out.println("开始组装欧尚轮胎"); + car.setTire("欧尚轮胎"); + System.out.println("欧尚轮胎组装完成"); + } + + @Override + public void setSeat() { + System.out.println("开始组装欧尚座位"); + car.setSeat("欧尚座位"); + System.out.println("欧尚座位组装完成"); + } +}