mercredi 31 mars 2021

Design Pattern for converting Java Data Model to XLS Workbook Sheet

Basically, I want to convert Java Object Model into two different sheets of an XLS workbook. I have solved this problem so far but I am trying to find better solution in terms of design pattern.

Here is the response Object Model.

class Response {
  List<Model1> list;
}

class Model1 {
  String attr1;
  String attr2;
  List<Model2> list;

  @JsonProperty(value="attr1")
  String getAttr1() {..}
}

class Model2 {
  String attr1;
  String attr2;

  @JsonProperty(value="attr1")
  String getAttr1() {..}
}

For conversion layer, I created a parent interface with different implementation as below.

public interface Converter<T> {
  Sheet convert(T t);
}

public class DataConverter {

   public Workbook getWorkBook(Response res) {
      Sheet model1Sheet = new Model1SheetConverter().convert(res);
      Sheet model2Sheet = new Model2SheetConverter().convert(res);
      return new Workbook().sheets(List.of(model1Sheet, model2Sheet));
   }
}

public class Model1SheetConvert implements Converter<Response> {
   public sheet convert(Response res) {
       I do loop over here for data model 1 and then with some business logic generate sheet.
   }
}

public class Model2SheetConvert implements Converter<Response> {
   public sheet convert(Response res) {
       I do loop over here for each data model 1 then I pass data model 2 and then with some business logic generate sheet.
   }
}

Now problem with this approach are:

  1. I am iterating two times for data model 1.
  2. There will be some dynamic rows for sheet 1 and sheet 2 based on data model 1. I can not handle this part here with this approach.

Can someone please suggest a good design pattern for this approach? Or, is there is any other way, I can do it differently.

Aucun commentaire:

Enregistrer un commentaire