0

Client needs me to provide two different classes Service1Transformer and Service2Transformer. Both classes need to implement the interface Transformer which only contains a transform() method. Now after writing all the logic for transformation, it is the exact same for Service1 and 2, with the difference being in a large list that carries specifications (hardcoded specs) and the output of transformation. So I created an abstract class with all the methods implemented, and just left the method that generates the specs/output abstract. The 2 classes extend that base class and only implement the specs/output generation method. Specs/output are just a nested list of lists and pairs.

Unfortunately the checkstyle validator setup for that code base refuses my spec/output generation method because it is more than 100 lines long (it’s quite a long list). So looks like I need to make these specs/output become a field rather than getting generated by a method. But I can’t do that because of field hiding.

Any thoughts on what a good approach to this design issue would be?

interface Transformer {
    public String transform();
}

abstract class ServiceTransformer implements Transformer {
    public String transform() {…}
    public abstract List<Pair<String,List<String>>> generateTransformationSpecsAndOutputContainer();

}

class Service1Transformer extends ServiceTransformer {
    public List<Pair<String,List<String>>> generateTransformationSpecsAndOutputContainer() {…}
}

class Service2Transformer extends ServiceTransformer {
    public List<Pair<String,List<String>>> generateTransformationSpecsAndOutputContainer() {…}
}
A K
  • 737
  • 2
  • 8
  • 17
  • Your `Service1Transformer` and `Service2Transformer` have `abstract` methods, but those methods have bodies, and the class isn't abstract... – tgdavies Jan 28 '23 at 06:30
  • Thanks for catching it. Fixed the typo. This is pseudocode as I can’t share actual code and it is a design question. – A K Jan 28 '23 at 06:36
  • See this: https://stackoverflow.com/questions/4023185/disable-a-particular-checkstyle-rule-for-a-particular-line-of-code – tgdavies Jan 28 '23 at 06:40
  • Thanks. Unfortunately I can’t use this approach. I need to change the design. I’m not sure my original design was that good. Therefore want to hear what other think would be a good approach. – A K Jan 28 '23 at 06:44

0 Answers0