程式碼實現
- 下面是一個簡單的Java建造者模式的案例,這個案例展示了一個Car類(產品類),由CarBuilder介面(構建器介面)和多個實現了這個介面的SedanCarBuilder、SUVCarBuilder類(具體構建器類)來構建。
- 首先是Car類(產品類):
public class Car {
private String model;
private String engine;
private String transmission;
public Car(String model, String engine, String transmission) {
this.model = model;
this.engine = engine;
this.transmission = transmission;
}
public Car(){
}
public String showCarDetail(){
System.out.println("Car Model: "+model);
System.out.println("Engine: "+engine);
System.out.println("Transmission: "+transmission);
return "haha";
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
public String getTransmission() {
return transmission;
}
public void setTransmission(String transmission) {
this.transmission = transmission;
}
@Override
public String toString() {
return "Car{" +
"model='" + model + '\'' +
", engine='" + engine + '\'' +
", transmission='" + transmission + '\'' +
'}';
}
}
public interface CarBuilder {
void setModel(String model);
void setEngine(String engine);
void setTransmission(String transmission);
Car build();
}
- 然後是實現CarBuilder介面的SedanCarBuilder(具體構建器類)
public class SedanCarBuilder implements CarBuilder{
private Car car;
public SedanCarBuilder(){
this.car = new Car();
}
@Override
public void setModel(String model) {
car.setModel(model);
}
@Override
public void setEngine(String engine) {
car.setEngine(engine);
}
@Override
public void setTransmission(String transmission) {
car.setTransmission(transmission);
}
@Override
public Car build() {
return car;
}
}
- 實現CarBuilder介面的SUVCarBuilder類
public class SUVCarBuilder implements CarBuilder{
private Car car;
public SUVCarBuilder (){
this.car = new Car();
}
@Override
public void setModel(String model) {
car.setModel(model);
}
@Override
public void setEngine(String engine) {
car.setEngine(engine);
}
@Override
public void setTransmission(String transmission) {
car.setTransmission(transmission);
}
@Override
public Car build() {
return car;
}
}
- 最後是Director類(指揮者類),它使用構建器物件來構建產品
public class Director {
private CarBuilder carBuilder;
public Director(CarBuilder carBuilder){
this.carBuilder = carBuilder;
}
public void constructSedanCar(){
carBuilder.setModel("Sedan Model X");
carBuilder.setEngine("2.5L Trubo");
carBuilder.setTransmission("Semi-Automatic");
Car car = carBuilder.build();
car.showCarDetail();
System.out.println(car.toString());
System.out.println("====");
}
public void constructSUVCar(){
carBuilder.setModel("SUV Model Y");
carBuilder.setEngine("3.5L V6");
carBuilder.setTransmission("4x4");
Car car = carBuilder.build();
car.showCarDetail();
System.out.println(car.toString());
}
}
public class Client {
public static void main(String[] args) {
//構建Sedan車型
SedanCarBuilder sedanCarBuilder = new SedanCarBuilder();
Director director = new Director(sedanCarBuilder);
director.constructSedanCar();
//構建SUV車型
SUVCarBuilder suvCarBuilder = new SUVCarBuilder();
director = new Director(suvCarBuilder);
director.constructSUVCar();
}
}