vendredi 27 janvier 2023

Two classes with exact same methods and field. Only 1 field has different value

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 abstract List<Pair<String,List<String>>> generateTransformationSpecsAndOutputContainer() {…}
}

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

Aucun commentaire:

Enregistrer un commentaire