7

我正在阅读 Effective Java 书,并为我的未来参考创建笔记,我遇到了 Builder Pattern。

好吧,我了解它是什么以及它应该如何使用。在此过程中,我创建了构建器模式的两个示例变体。

我需要帮助来列出差异和各自的优势吗?好吧,我当然注意到,Example 1公开的方法更少,限制更少,更通用,允许更灵活地使用。

请指出我错过的其他事情?

示例 1

package item2;

/**
 * @author Sudhakar Duraiswamy
 *
 */
public  class Vehicle {

    private String type;
    private int wheels;

    interface Builder<T>{
        public  T build();
    }

    public static class CarBuilder implements Builder<Vehicle>{
        private String type;
        private int wheels;     

        CarBuilder createVehicle(){
            this.type= "Car";
            return this;
        }

        CarBuilder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

        public Vehicle build(){
            Vehicle v = new Vehicle();
            v.type = type;
            v.wheels = wheels;
            return v;
        }               
    }

    public static class TruckBuilder implements Builder<Vehicle>{       
        private String type;
        private int wheels; 

        TruckBuilder createVehicle(){           
            this.type= "Truck";
            return this;
        }

        TruckBuilder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

        public Vehicle build(){
            Vehicle v = new Vehicle();
            v.type = type;
            v.wheels = wheels;
            return v;
        }
    }   

    public Vehicle(){

    }

    public static void main(String[] args) {
        //This builds a car with 4 wheels
        Vehicle car = new Vehicle.CarBuilder().createVehicle().addWheels(4).build();

        //THis builds a Truck with 10 wheels
        Vehicle truck = new Vehicle.TruckBuilder().createVehicle().addWheels(10).build();

    }
}

示例 2

package item2;
/**
 * @author Sudhakar Duraiswamy
 *
 */
public  class Vehicle2 {

    private String type;
    private int wheels;

    interface Builder<T>{
        public  T build();      
        public String getType();
        public int getWheels() ;
    }

    public static class CarBuilder implements Builder<Vehicle2>{
        private String type;
        private int wheels;     

        public String getType() {
            return type;
        }
        public int getWheels() {
            return wheels;
        }

        CarBuilder createVehicle(){
            this.type= "Car";
            return this;
        }

        CarBuilder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

        public Vehicle2 build(){        
            return new Vehicle2(this);
        }               
    }

    public static class TruckBuilder implements Builder<Vehicle2>{      
        private String type;
        private int wheels; 

        public String getType() {
            return type;
        }

        public int getWheels() {
            return wheels;
        }

        TruckBuilder createVehicle(){           
            this.type= "Truck";
            return this;
        }

        TruckBuilder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

        public Vehicle2 build(){
            return new Vehicle2(this);
        }
    }


public Vehicle2(Builder<? extends Vehicle2> builder){
    Vehicle2 v = new Vehicle2();
    v.type = builder.getType();
    v.wheels = builder.getWheels();
}

    public Vehicle2(){
    }

    public static void main(String[] args) {            
        //This builds a car with 4 wheels
        Vehicle2 car = new Vehicle2.CarBuilder().createVehicle().addWheels(4).build();

        //THis builds a Truck with 10 wheels
        Vehicle2 truck = new Vehicle2.TruckBuilder().createVehicle().addWheels(10).build();
    }
}
4

2 回答 2

9

以上都不是。

第一个不允许构建不可变的 Vehicle,这通常是使用 Builder 模式的原因。

第二个示例是第一个示例的变体,它允许使用其他 getter 方法从构建器获取信息。但是这些方法并没有在任何地方使用,除了在 Vehicle 构造函数中,它可以直接访问构建器字段。我看不出添加它们的意义。

我发现有两件更重要的事情需要改进:

  1. 这两种构建器类型做的事情完全相同。不需要两种类型。一个就足够了。
  2. createVehicle()方法的作用应该由构建器构造函数完成。如果你构建一个CarBuilder,显然是在构建一辆汽车,所以在构建器构建的时候就应该设置好车辆的类型。我会这样写:

.

public final class Vehicle {

    private final String type;
    private final int wheels;

    private Vehicle(Builder builder) {
        this.type = builder.type;
        this.wheels = builder.wheels;
    }

    public static Builder carBuilder() {
        return new Builder("car");
    }

    public static Builder truckBuilder() {
        return new Builder("truck");
    }

    public static class Builder {
        private final String type;
        private int wheels;

        private Builder(String type) {
            this.type = type;
        }

        public Builder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

        public Vehicle build() {
            return new Vehicle(this);
        }               
    }

    public static void main(String[] args) {
        Vehicle car = Vehicle.carBuilder().addWheels(4).build();
        Vehicle truck = Vehicle.truckBuilder().addWheels(10).build();
    }
}
于 2013-03-17T13:36:13.317 回答
3

还有第三种变体,代码更少:

除了拥有自己的实例字段外,构建器还可以改变Vehicle. 内部类可以编写其外部类的私有成员:

class Vehicle {
  private int wheels;

  private Vehicle() {}

  public static class Builder {
    private boolean building = true;
    private Vehicle vehicle = new Vehicle();

    public Builder buildWheels(int wheels) {
      if(!this.building) throw new IllegalStateException();
      this.vehicle.wheels = wheels;
      return this;
    }

    public Vehicle build() {
      this.building = false;
      return this.vehicle;
    }
  }
}

由于字段是私有的,并且您只允许构建一次building标志),因此即使字段不再是,构建Vehicle的实例仍然对消费者不可变final(不再具有realio-trulio 不变性,请参阅 Eric 的博客文章,该文章位于 C# 但概念类似)。

您需要更加小心,因为在对象构造期间不必初始化非最终字段(由编译器强制执行),并且您必须building仔细检查状态。但是,您确实保存了所有实例字段的完整副本。通常,如果您有一组相当大的实例变量,这些实例变量是用很少的方法构建的,其中每个方法一次构建几个字段,这很有用。

我知道这并没有指出您的方法的任何优点或缺点。但是,如果您不需要字段,这种方法可以节省大量额外的代码final

于 2013-03-17T13:35:44.020 回答