Generic Builder in Java

I created a constructor for the lookup table and used it as shown below.

public class RaceCodeDataBuilder {

    private RaceCode raceCode;  

    public RaceCodeDataBuilder() {
        raceCode = new RaceCode();
    }

    public RaceCodeDataBuilder code(String code) {
        raceCode.setCode(code);     
        return this;
    }

    public RaceCodeDataBuilder displayName(String displayName) {
        raceCode.setDisplayName(displayName);
        return this;
    }

    public RaceCode build() {
        return raceCode;
    }

}

Using this constructor in a test:

   RaceCode mockRaceCode = new RaceCodeDataBuilder()
                         .code("2054-5")
                         .displayName("Black or African American")
                         .build();

I expect that other similar builders will have other similar tables, such as StateCodeBuilder, GenderCodeBuilder, and all of them have only "code" and "displayName", similar to the constructor.

I want to create a common constructor and not create several constructor classes that perform the same task with a different name.

I tried something in generics, but I'm leaving.

public class CodeDataBuilder<T>{

    private T t;    

    public CodeDataBuilder(T t) {
        this.t = t;
    }

    public CodeDataBuilder code(String code) {
        raceCode.setCode(code);     // Cant write T.setCode here for obvious resons
        return this;
    }

    public CodeDataBuilder displayName(String displayName) {
        raceCode.setDisplayName(displayName); // Cant write T.setDisplayNamehere for obvious resons
        return this;
    }

    public T build() {
        return t;
    }

}

Can someone help me?

Thank.

+3
source share
5 answers

Create an interface BuildableCodeDatausing the necessary methods and implement it using type classes RaceData.

:

public interface BuildableCodeData {

  public void setCode(String code);

  public void setDisplayName(String name);
}

public class Builder<T extends BuildableCodeData> {
  private T codeData;

  public Builder(T codeData) {
    this.codeData = codeData;
  }

  public Builder<T> setCode(String code) {
    codeData.setCode(code);
    return this;
  }

  public Builder<T> setDisplayName(String displayName) {
    codeData.setDisplayName(displayName);
    return this;
  }

  public T build() {
    return codeData;
  }
}
+3

, , . :

public interface Buildable{
    void setDisplayName(String name);
    void setCode(String code);
}

public class CodeDataBuilder {

    private Buildable mObj;    

    public CodeDataBuilder(Buildable mObj) {
        this.mObj = mObj;
    }

    public CodeDataBuilder code(String code) {
        mObj.setCode(code);     // Cant write T.setCode here for obvious resons
        return this;
    }

    public CodeDataBuilder displayName(String displayName) {
        mObj.setDisplayName(displayName); // Cant write T.setDisplayNamehere for obvious resons
        return this;
    }

    public Buildable build() {
        return mObj;
    }

}
}

, , Buildable.

+2

:

interface CodeModel {
  public void setCode(String s);
  public void setDisplayName(String s);
}

T extends CodeModel, :

class CodeDataBuilder<T extends CodeModel> {
  // T has setCode method now!
}

, !

+1

, . , .

:

public interface CodeNameable  {
   String getCode();
   String getName();
}

:

public class CodeNamedCar implements CodeNameable  {
   private String code;
   private String name;
   public CodeNamedCar(String code, String name)  {
      this.code = code;
      this.name = name;
   }
}

:

public abstract class CodeNameBuilder<C extends CodeNameable>  {
   public String code;
   public String name;
   public CodeNameBuilder()  {
   }
}

:

public abstract class CarBuilder extends CodeNameBuilder<CodeNamedCar>  {
   public CarBuilder()  {
   }
   public CarBuilder code(String co_de)  {
      this.code = code;
      return  this;
   }
   public CarBuilder name(String name)  {
      this.name = name;
      return  this;
   }
   public CodeNameCar build()  {
      return  (new CodeNameCar(code, name));
   }
}

, :

CodeNamedCar car = new CarBuilder().code("thecode").name("Mazda").build();

, (, ) CodeNameCar. .

0

- , .

, , , , , .

, Nameable .

, .

class Code {
  int number; 
  String name;
}

class Race {
  Code code;
  //other attributes; 
}

Then you have one builder for the code, and another for the race.

We also note that good design is a balance of trading positions. If two fields are common for 5 classes, it is the goal to make the code complex and create a special mechanization that will only package initialization and will not do anything productive.

0
source

All Articles