mercredi 4 novembre 2020

Can helper class in Java have state or member fields inside it?

I have a several calculator classes that that computes something for user.

@Getter
@Setter
public class Input{

    private Info info;
    private ResultAlpha resultAlpha;
    private ResultBeta resultBeta;
    private ResultGamma resultGamma;
    private ResultPsi resultPsi;
    private ResultLambda resultLambda;
    private ResultAph resultAph;

}

public class CalculationHelper(){
    

    private Info info;
    private ResultAlpha resultAlpha;
    private ResultBeta resultBeta;
    private ResultGamma resultGamma;
    private ResultPsi resultPsi;
    private ResultLambda resultLambda;
    private ResultAph resultAph;


    CalculationHelper(Input input){
        info = input.getInfo;
        resultAlpha = input.getResultAlpha();
        resultBeta =input.getResultBeta();
        resultGamma=input.getResultGamma();
        resultAph = input.getResultAph();
        resultLambda =input.getResultLambda();
        resultPsi = input.getResultPsi();
    }

    public void calculate(){

        calculate1();
        calculate2();
        calculate3();
        calculate4();
        calculate5();
        calculate6();

    }

    private void calculate1(){

    }

    private void calculate2(){

    }

    private void calculate3(){

    }

    private void calculate4(){

    }

    private void calculate5(){

    }

    private void calculate6(){

    }

}


I did not want to pass all the variables into calculate method so I have saved a reference of all the fields inside Input class in the calculator class. This helps me to avoid sending all the arguments inside every calculate function. But I will have to make a new object for CalculationHelper every time I call it for a new user, otherwise it will not be thread safe.

One alternative I have is the following approach -

public final class CalculationHelper(){


    private CalculationHelper(){
    
    }

    public static void calculate(Input input){
   

        Info info = input.getInfo();
        ResultAlpha resultAlpha = input.getResultAlpha();
        ResultBeta resultBeta =input.getResultBeta();
        ResultGamma resultGamma=input.getResultGamma();
        ResultAph resultAph = input.getResultAph();
        ResultLambda resultLambda =input.getResultLambda();
        ResultPsi resultPsi = input.getResultPsi();

        calculate1(info, resultAlpha, resultBeta, resultGamma, resultAph, resultLambda, resultPsi);
        calculate2(info, resultAlpha, resultBeta, resultGamma, resultAph, resultLambda, resultPsi);
        calculate3(info, resultAlpha, resultBeta, resultGamma, resultAph, resultLambda, resultPsi);
        calculate4(info, resultAlpha, resultBeta, resultGamma, resultAph, resultLambda, resultPsi);
        calculate5(info, resultAlpha, resultBeta, resultGamma, resultAph, resultLambda, resultPsi);
        calculate6(info, resultAlpha, resultBeta, resultGamma, resultAph, resultLambda, resultPsi);

    }

    private static void calculate1(Info, ResultAlpha, ResultBeta, ResultGamma, ResultAph, ResultLambda, ResultPsi){

    }

    private static void calculate2(Info, ResultAlpha, ResultBeta, ResultGamma, ResultAph, ResultLambda, ResultPsi){

    }

    private static void calculate3(Info, ResultAlpha, ResultBeta, ResultGamma, ResultAph, ResultLambda, ResultPsi){

    }

    private static void calculate4(Info, ResultAlpha, ResultBeta, ResultGamma, ResultAph, ResultLambda, ResultPsi){

    }

    private static void calculate5(Info, ResultAlpha, ResultBeta, ResultGamma, ResultAph, ResultLambda, ResultPsi){

    }

    private static void calculate6(Info, ResultAlpha, ResultBeta, ResultGamma, ResultAph, ResultLambda, ResultPsi){

    }

}

This would not require to create an instance of object of CalculationHelper class.

My main problem is that since Input class is a deeply nested object, I want to avoid lot of getter functions when trying to use data from Input class. And that is the main reason why I tried to break the Input objects into its member fields. Otherwise, my calculation function will have long getter calls like

input.getResultAlpha().getObj2().getObj3().getObj4().setData(1234);

So is it okay to save state in Helper classes as I did in the first approach?

Aucun commentaire:

Enregistrer un commentaire