dimanche 10 novembre 2019

How to add conditions dynamically according to the different requirement?

I am writing a code in Java to enable different components. Every component needs to satisfy a certain set of conditions to get enabled. Every condition is basically a method which performs some logic & returns boolean. Currently, I have written a code in which there is a different method to perform enable logic for each component & that corresponding method performs the conditional operations to return boolean. My current implementation of enabling different components looks something like this :

private boolean shouldEnableComponent1() {
    String param1 = getParam1();
    String param2 = getParam2();
    String param3 = getParam3();
    if(checkCondition1(param1) && checkCondition2(param2) && checkCondition3(param3) {
       return true;
    } else {
        return false;
    }
}

private boolean shouldEnableComponent2() {
    String param1 = getParam1();
    String param2 = getParam2();
    String param4 = getParam4();
    if(checkCondition1(param1) && checkCondition2(param2) && checkCondition4(param4) {
       return true;
    } else {
        return false;
    }
}

Note that different shouldEnableComponentN() methods may have to call same conditional methods like in above case both shouldEnableComponent1() & shouldEnableComponent2() are calling checkCondition1() & checkCondition2() methods. The difference between their enabling condition is the 3rd conditional check i.e. checkCondition3() in case of component1 & checkCondition4() in case of component2.
This is the most obvious implementation which I could come up with but it has few problems -
Duplicity - Since there is an overlap between conditional checks of different components, that part of the code is duplicated.
Scalability - Today there are 3 components, tomorrow there will be 10 components. So, I will need to implement 10 different methods for each components. That's definitely bad.

I am thinking of Strategy pattern for this use case but I am not sure how to implement it here. Is strategy pattern the correct choice? If yes, please give high level idea of how it will be implemented i.e. where will I put the common variables which need to be used across classes, where will the common logic part of all concrete classes be placed etc. Or is there any other design pattern which will be better than strategy pattern for this use case? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire