mardi 4 janvier 2022

Java Class with a lot of fields [closed]

I need to design a class with a lot of fields to receive some optional and mandatory data. For the mandatory fields i use Fluent or Step Builder design pattern, as i understand it’s the same. But for the optional fields I also need to design some structure, group them, that is convenient to use my class. What would be the best options?

For example, there is a Data class with many optional fields.

  @Accessors(chain = true)
  @Setter
  public class Data{
    String A;
    String B;
    String C;
    String D;
    String E;
    String F;
  }

I could split fields in Group1 and Group2 logically. And use setter like in the class Data1 or final fields like in the class Data2.

  @Accessors(chain = true)
  @Setter
  public class Data1{
    Group1 group1;
    Group2 group2;
  }

  public class Data2{
    public final Group1 group1=new Group1();
    public final Group2 group2=new Group2();
  }

  @Accessors(chain = true)
  @Setter
  public class Group1{
    String A;
    String B;
    String C;
  }

  @Accessors(chain = true)
  @Setter
  public class Group2{
    String D;
    String E;
    String F;
  }

Another questing is what would be the best naming for a setter setA(…), withA(…) or just a(…)

Aucun commentaire:

Enregistrer un commentaire