层次构建器模式的实现

Implementation of Hierarchical Builder pattern

我正在实施分层构建器模式。我已经使用泛型将 return 类型的父构建器 setter 设为子构建器的类型。在调用父 setter 方法后使用子 setter 方法时,我仍然面临问题。我不想在我的父生成器中定义我的子特定方法(此处为 setEngine(String))。这个问题还有其他解决办法吗?

我已经针对上述问题制作了一个示例片段,与本例相同。

CarFactory ->它return是用户想要的特定汽车的对象

汽车 -> 所有汽车类型的父项,Swift,除尘器等

Swift -> 具体车型

父级->子级

汽车 -> Swift

CarBuilder -> SwiftBuilder

Car.java

 package Builders;

 public class Car {
   int tyre;
   int seat;
   public int getTyre() {
       return tyre;
   }
   public void setTyre(int tyre) {
       this.tyre = tyre;
   }
   public int getSeat() {
       return seat;
   }
   public void setSeat(int seat) {
       this.seat = seat;
       
   }
   
}

Swift.java

package Builders;

public class Swift extends Car {
    boolean safetyAirbag;
    String engine;
    
    public boolean isSafetyAirbag() {
        return safetyAirbag;
    }

    public String getEngine() {
        return engine;
    }

    public void setSafetyAirbag(boolean safetyAirbag) {
        this.safetyAirbag = safetyAirbag;
    }

    public void setEngine(String engine) {
        this.engine = engine;
    }


}

CarBuilder.java

package Builders;

public abstract class CarBuilder {
        int tyre;
        int seat;
         
        public abstract <B extends CarBuilder>B self();
        public abstract <T extends Car>T typeOfCar();
            
        public <B extends CarBuilder>B setTyre(int tyre) {
            this.tyre = tyre;
            return self();
        }
        
        public  <B extends CarBuilder> B setSeat(int seat) {
            this.seat = seat;
            return self();
        }
        public <C extends Car>C build()
        {   C car=this.typeOfCar();
            car.setSeat(seat);
            car.setTyre(tyre);
            return car;
        }
    
        
}

SwiftBuilder.java

package Builders;

public class SwiftBuilder extends CarBuilder {
    String engine;
    @Override
    public
    SwiftBuilder self() {
        
        return this;
    }

    @Override
    public
    Swift typeOfCar() {
        
        return new Swift();
    }

    public SwiftBuilder setEngine(String string) {
        this.engine=string;
        return this;
    }
    public Swift build()
    {   Swift s=(Swift)super.build();
        return s;
    }

}

CarFactory.java

package Builders;

public class CarFactory {
    public SwiftBuilder getSwiftDesire()
    {
        return new SwiftBuilder();
    }
}
 

Drivers.java

package Builders;

public class Drivers {
    Swift getMyCar() {
        Swift s= this.factory().getSwiftDesire().setSeat(4).setEngine("CC").build();
        return s;
    }
    CarFactory factory() {
        return new CarFactory();
    }
}

在 Drivers.java class 我无法在 setSeat() 方法之后使用 setEngine() 方法, this.factory().getSwiftDesire().setSeat(4).setEngine("CC").build(); 我不想在父 class 中声明 setEngine,有什么办法可以做到这一点吗?

提前致谢!

您需要在 CarBuilder 中的 class 级别而不是方法级别使用泛型:

package Builders;

public abstract class CarBuilder<B extends CarBuilder<B, C>, C extends Car> {
    int tyre;
    int seat;

    public abstract B self();

    public abstract C typeOfCar();

    public B setTyre(int tyre) {
        this.tyre = tyre;
        return self();
    }

    public B setSeat(int seat) {
        this.seat = seat;
        return self();
    }

    public C build() {
        C car = this.typeOfCar();
        car.setSeat(seat);
        car.setTyre(tyre);
        return car;
    }

}

然后定义 SwiftBuilder:

package Builders;

public class SwiftBuilder extends CarBuilder<SwiftBuilder, Swift> {
    String engine;

    @Override
    public SwiftBuilder self() {

        return this;
    }

    @Override
    public Swift typeOfCar() {

        return new Swift();
    }

    public SwiftBuilder setEngine(String string) {
        this.engine = string;
        return this;
    }

    public Swift build() {
        Swift s = super.build();
        s.setEngine(engine);
        return s;
    }

}

而且有效。