When target Objects excelColumns, pdfColumns partly share same Objects and some of them even conditionally, what is a good OOP pattern to void functional programming, tight coupling and boilerplate like in code below? Lets assume, that there will be a lot of shared columns and only few non shared and conditional ones.
List<Column> excelColumns = new ArrayList<>();
List<Column> pdfColumns = new ArrayList<>();
//shared columns
Column test = new Column("test", 121, 11);
excelColumns.add(test);
pdfColumns.add(test);
//conditional columns
if (condition) {
excelColumns.add(new Column("test2", 12, 21));
}
//non shared columns
pdfColumns.add(new Column("test3", 12, 41));
//shared columns
Column test4 = new Column("test4", 12, 331);
excelColumns.add(test4);
pdfColumns.add(test4);
Column test5 = new Column("test5", 72, 11);
excelColumns.add(test5);
pdfColumns.add(test5);
Column test6 = new Column("test6", 82, 121);
excelColumns.add(test6);
pdfColumns.add(test6);
Depending on your appetite for complexity, you can do the following:
HeaderColumns,BodyColumns, etc.Here is a possible implementation of the pattern following the suggestions above:
You could use it as follows:
Advantages of this approach:
Visitor, it will generate a compile error in all visitor implementations. That avoids typical programming mistakes where people forget to add branches toiforswitchstatements.Disadvantages of this approach:
Columninstances into semantically cohesive objects. For example, you might end with weirdness inReportHeaderlike this: