vendredi 31 mars 2017

Java: design-pattern for building certain columns in certain order

I have a table with N columns (up to 20-25). The list of columns (columns and their order) is set by user. So this list is unknown in code writing time.

I have the following solution:

class abstract AbstractColumnBuilder{

  public TableColumn buildColumnA(){...}

  public TableColumn buildColumnB(){...}

  public TableColumn buildColumnC(){...}

  public TableColumn buildColumn(String columnName){
     switch(columnName){
         case "a":return buildColumnA();
         case "b":return buildColumnB();
         case "c":return buildColumnC();
     }
     return null;
  }
}

class ConcreteColumnBuilder extends AsbtractColumnBuilder{

 public TableColumn buildColumnD(){...}

 public TableColumn buildColumnE(){...}

 @Override
 public TableColumn buildColumn(String columnName){
     switch(columnName){
         case "d":return buildColumnD();
         case "e":return buildColumnE();
         default:return super.buildColumn(columnName);
     }
     return null;
  }
}

and the usage

List<String>columnNames=...
for(String columnName:columnNames){
   TableColumn column=builder.buildColumn(columnName);
}

However, I don't like this solution. First of all it seems to me it is not very good from perfomance point of view - we need to make long case check in loop. Secondly developer can forget to override buildColumn method.

Is there any patterns for such case?

Aucun commentaire:

Enregistrer un commentaire