feat: 建造者模式

This commit is contained in:
裴浩宇 2023-10-30 08:45:05 +08:00
parent 2e9eff0ead
commit 25cebacd80
6 changed files with 129 additions and 10 deletions

View File

@ -6,7 +6,27 @@ package com.pnkx.designpatterns.builder;
* @author 裴浩宇 * @author 裴浩宇
* @version 1.0 * @version 1.0
* @date 2023/10/27 15:15 * @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("比亚迪座位组装完成");
}
} }

View File

@ -14,25 +14,25 @@ public abstract class Builder {
protected Car car = new Car(); 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 * @return
*/ */
public Car getCar() { public Car outputCar() {
return car; return car;
} }
} }

View File

@ -40,4 +40,13 @@ public class Car {
public void setSeat(String seat) { public void setSeat(String seat) {
this.seat = seat; this.seat = seat;
} }
@Override
public String toString() {
return "Car{" +
"engine='" + engine + '\'' +
", tire='" + tire + '\'' +
", seat='" + seat + '\'' +
'}';
}
} }

View File

@ -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();
}
}

View File

@ -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());
}
}

View File

@ -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("欧尚座位组装完成");
}
}