mercredi 22 septembre 2021

code refactoring for data aggregation service in java

I have the following situation in my application:

  • I have a service class with 700+ lines
  • The class is responsible for data aggregation from different sources.
  • There is no real signature in the inputs, as I mentioned in the code example.
  • The service will collect the data sequentially and sometimes we should ignore some method based on specific condition.
  • Currently the code become unmaintainable since the logic is growing and become more and more complex.
  • I already have looked into pipeline and Chain of Responsibility design pattern
    • pipeline: I think it will not resolve my issue, since the output from step1 is not necessary to be an input for step2
    • Chain of Responsibility: also this pattern will not fix the issue since the input signature is different.

Could you please suggest away to refactor the code in order to make things clear and maintainable.

public class DataCollector{

    public Report collect(input1){
        Report report = new Report();
        report.setFirstPart(getFirstPart(input1));
        report.setSecondPart(getSecondPart(report.getFirstPart().getInput2()));
        report.setThirdPart(getThirdPart(input1, report.getSecondPart().getInput3()));
        report.setFourthPart(input1, report.getFirstPart().getInput2());
        if( input1 > report.getSecondPart().getInput3()){
            report.setFifthPart(report.getFirstPart().getInput2());
        }
        else{
            report.setSixthPart(input1, report.getSecondPart().getInput3(), report.getThirdPart().getInput4());
        }
        return report;
    }


    public FirstPart getFirstPart(input1){
        ...method logic(reterive from DB or call service either rest or soap)
    }

    public SecondPart getSecondPart(input2){
        ...method logic(reterive from DB or call service either rest or soap)
    }

    public ThirdPart getThirdPart(input1, input3){
        ...method logic(reterive from DB or call service either rest or soap)
    }

    public FourthPart getFourthPart(input1, input2){
        ...method logic(reterive from DB or call service either rest or soap)
    }

    public FifthPart getFifthPart(input2){
        ...method logic(reterive from DB or call service either rest or soap)
    }

    public sixthPart getSixthPart(input1, input3, input4){
        ...method logic(reterive from DB or call service either rest or soap)
    }

}

Aucun commentaire:

Enregistrer un commentaire