Java摘要,泛型和构建器

时间:2016-09-18 16:53:57

标签: java generics

这是我第一次发帖,但我在遇到问题时遇到了很多麻烦。 我目前有一个带有标题的AbstractDevice类:

public abstract class AbstractDevice<T extends AbstractDevice.Builder<T>> implements Device

并且此类有一个带有标题的嵌套构建器类:

public static abstract class Builder<T>

我还有一个带有标题的AbstractPeripheral类:

public abstract class  AbstractPeripheral<T extends AbstractPeripheral.Builder<T>> extends AbstractDevice<AbstractPeripheral.Builder>

此类也有自己的嵌套构建器类,其标题为:

public static abstract class Builder<T> extends AbstractDevice.Builder<Builder>.

我的目标是让AbstractPeripheral扩展AbstractDevice和AbstractPeripheral的构建器扩展AbstractDevice。但是,我在尝试编译时收到此错误:

  

类型参数uxb.AbstractPeripheral.Builder不在类型变量T的范围内。

感谢任何帮助。谢谢 抽象装置:

package uxb;

import java.util.List;
import java.util.Optional;
import java.util.ArrayList;
import java.math.BigInteger;


public abstract class AbstractDevice<T extends AbstractDevice.Builder<T>>     
implements Device{

public static abstract class Builder<T>{

private Integer version;

private Optional<Integer> productCode;

private Optional<BigInteger> serialNumber;

private List<Connector.Type> connectors;

public Builder(Integer version){
  this.version = version;
}  //end constructor method


public T productCode(Integer productCode){
  if(productCode != null){
    this.productCode = Optional.of(productCode); 
  }  //end if statement
  else{
    this.productCode = Optional.empty();
  }  //end else statement
  return getThis();
}  //end method productCode()

public T serialNumber(BigInteger serialNumber){
  if(serialNumber != null){
    this.serialNumber = Optional.of(serialNumber);
  }  //end if statement
  else{
  /*Class has a static field for ZERO value*/
    this.serialNumber = Optional.empty();
  }  //end else statement
  return getThis();
}  //end method serialNumber()


public T connectors(List<Connector.Type> connectors){
 this.connectors = connectors;
 return getThis();
}  //end method connectors


protected abstract T getThis();


protected List<Connector.Type> getConnectors(){
  return connectors;
}  //end method getConnectors()


protected void validate(){
  if(version == null){
    throw new NullPointerException("Cannot be validated");
  }
}  //end method validate()

}  //end nested abstract class Builder


private final Integer version;

private final Optional<Integer> productCode;


private final Optional<BigInteger> serialNumber;

private final List<Connector.Type> connectors;

private final List<Connector> connectorObjects;


protected AbstractDevice(Builder<T> builder){
  this.version = builder.version;
  this.productCode = builder.productCode;
  this.serialNumber = builder.serialNumber;
  this.connectors = builder.connectors;
  ArrayList<Connector> temp = new ArrayList<Connector>();
  for(int i = 0; i < connectors.size(); i++){
    temp.add(new Connector(this, i, connectors.get(i)));
  }  //end for loop
  connectorObjects = temp;
}  //end constructor method



public Optional<Integer> getProductCode(){
return productCode;
}  //end method getProductCode()


public Integer getConnectorCount(){
/*Not Implemented Yet*/
return 0;
}  //end method getConnectorCount()



public Optional<BigInteger> getSerialNumber(){
return serialNumber;
}  //end method getSerialNumber()


public Integer getVersion(){
return version;
}  //end method getVersion()

public List<Connector> getConnectors(){
return new ArrayList<Connector>(connectorObjects);
}  //end method getConnectors()


public Connector getConnector(int index){
  if(! getConnectors().isEmpty()){
  return getConnectors().get(index);
}  //end if statement
else{
  return null;
}  //end else statement
}  //end method getConnector()

} //end abstract class AbstractDevice

摘要外围设备:     包uxb;

import java.util.List;

public abstract class  AbstractPeripheral<T extends  
AbstractPeripheral.Builder<T>> extends 
AbstractDevice<AbstractPeripheral.Builder>{

public static abstract class Builder<T> extends 
AbstractDevice.Builder<Builder>{

protected void validate(){
  super.validate();
  if(getConnectors().equals(null)){
    throw new IllegalStateException("Cannot be validated");
  }  //end if statement
  if(checkTypes(getConnectors())){
    throw new IllegalStateException("Cannot be validated");
  }  //end if statement
}  //end method 

private boolean checkTypes(List<Connector.Type> types){
  for(Connector.Type type: types){
    if(type != Connector.Type.PERIPHERAL){
      return false;
    }  //end if statement
  }  //end for each loop
  return true;
}  //end method checkTypes

public Builder(Integer version){
  super(version);
}  //end constructor method
}  //end nested class Builder

public AbstractPeripheral(Builder<T> builder){
 super(builder);
} 
}  //end class AbstractPeripheral

集线器:     包uxb;

public class Hub extends AbstractDevice<Hub.Builder>{

  public static class Builder extends AbstractDevice.Builder<Builder>{

public Builder(Integer version){
  super(version);
} //end constructor method

public Hub build(){
 validate();
 return new Hub(getThis());
}  //end method build()

protected Builder getThis(){
  return this;
} //end method getThis()

protected void validate(){
  super.validate();
  if(!(super.getConnectors().contains(Connector.Type.COMPUTER))){
    throw new IllegalStateException("Cannot be validated");
  }  //end if statement\
  if(!(super.getConnectors().contains(Connector.Type.PERIPHERAL))){
    throw new IllegalStateException("Cannot be validated");
  }  //end if statement
}  //end validate()
}  //end nested class Builder

private Hub(Builder builder){
super(builder);
}  //end constructor method

public DeviceClass getDeviceClass(){
return DeviceClass.HUB;
}  //end method getDeviceClass()


}  //end class Hub

1 个答案:

答案 0 :(得分:2)

您确实需要使用正在构建的类<​​em>和构建器作为构建器上的参数。正在构建的对象需要了解构建器,因此不需要它作为参数。

我在几个开源项目中使用过这种模式或略有变化,包括Apache Brooklyn和jclouds。

因此,从您的示例中,更改父类如下:

public abstract class AbstractDevice implements Device {
    public static abstract class Builder<T, B> {
        public abstract B self();
        public abstract T build();
        public B example(String value) {
            // do something with value
            return self();
        }
    }
}

请注意,我还添加了一个抽象的build()方法,它将返回构建的对象。 self()方法是必需的,因为如果构建器方法返回this,则它将具有错误的类型。相反,每个构建器方法必须以return self();结尾,如所示的example(String value)方法。然后孩子变成:

public abstract class AbstractPeripheral extends AbstractDevice {
    public static abstract class Builder<T, B> extends AbstractDevice.Builder<T, B> {
    }
}

您可以看到TB泛型参数分别用于指向类的类型和构建器的类型。因此,要创建一个使用它们的具体类,应该创建这样的东西:

public class Hub extends AbstractPeripheral {
    public static class Builder extends AbstractPeripheral.Builder<Hub, Builder> {
        public static final Builder builder() {
            return new Builder();
        }
        public Builder self() {
            return this;
        }
        public Hub build() {
            return new Hub();
        }
    }
}

这也有一个静态builder()方法,它返回一个正确的Builder类的实例,你也可以直接调用构造函数。 build()方法只是创建并返回具体类,self()方法实现 here 以返回具有正确类型的this

将所有内容放在一起,我们可以使用它来创建Hub对象,如下所示:

Hub hub = Hub.Builder.builder()
        .example("something")
        .build();

请注意,AbstractDevice.Builder#example(String)方法返回正确的类型,因为它实际调用Hub#self()build()按预期返回Hub的具体实例。

为了解决这个问题并删除重复的样板,你也可以尝试使用Google AutoValue项目,我们现在正在jclouds中切换。