I need to enable 2 components of my software based on their eligibility to be enabled. The eligibility of a particular component is defined by few parameters. Hence, to enable a particular component, it should satisfy all the required parameters. I get these parameters from a Map. I loop over that map to check if for any key value pair of map, all the parameter satisfy the conditions. If yes, enable the component. Let's look at my code to have better understanding -
private boolean enableComponent1() {
String key = prefix1+"enableComponent1"; //prefix1 is a static string
Map<String, Object> inputMap = new HashMap<String, Object> ();
inputMap = useKeyToGetInputMap(key);
for(Map.Entry<String, Object> entry : inputMap.entrySet()) {
Object inputObject = entry.getValue();
String param1Value = inputObject.pram1;
String param2Value = inputObject.pram2;
String param3Value = inputObject.pram3;
if(satisfiesParam1(param1Value) && satisfiesParam2(param2Value) && satisfiesParam3(param3Value)) {
return true;
}
}
return false;
}
private boolean enableComponent2() {
String key = prefix2+"enableComponent2";//prefix2 is a static string
Map<String, Object> inputMap = new HashMap<String, Object> ();
inputMap = useKeyToGetInputMap(key);
for(Map.Entry<String, Object> entry : inputMap.entrySet()) {
Object inputObject = entry.getValue();
String param1Value = inputObject.pram1;
String param2Value = inputObject.pram2;
String param3Value = inputObject.pram3;
String param4Value = inputObject.pram4;
if(satisfiesParam1(param1Value) && satisfiesParam2(param2Value) && satisfiesParam3(param3Value) && satisfiesParam4(param4Value)) {
return true;
}
}
return false;
}
There are so many things which are common in both the methods. So, it makes sense to reuse someparts of the code.
The only differences in both the methods are -
- Key for getting inputMap is different.
-
Enabling of component 2 is dependent on 4 params whereas component 1 is dependent on 3 params.
So, to reuse the functionality I wrote a code which is like this -
private boolean enableComponent(String componentName) {
String key = "";
if(componentName.equals("component1")) {
key = prefix1+"enableComponent1"; //prefix1 is a static string
}
else {
key = prefix2+"enableComponent2"; //prefix2 is a static string
}
Map<String, Object> inputMap = new HashMap<String, Object> ();
inputMap = useKeyToGetInputMap(key);
for(Map.Entry<String, Object> entry : inputMap.entrySet()) {
Object inputObject = entry.getValue();
String param1Value = inputObject.pram1;
String param2Value = inputObject.pram2;
String param3Value = inputObject.pram3;
if(satisfiesParam1(param1Value) && satisfiesParam2(param2Value) && satisfiesParam3(param3Value)) {
if(componentName.equals("component2")) {
String param4Value = inputObject.pram4;
return satisfiesParam4(param4Value);
}
return true;
}
}
return false;
}
This solution works but I am not inclined to use this solution because it's definitely not scalable. Since in future, we may need to enable more components & those components may be requiring some additional parameteres to check or may be requiring lesser parameteres to check. So, to handle all those things, this code will quickly become very messy. Can there be any other approach/design pattern which I can follow to make it scalable?
Aucun commentaire:
Enregistrer un commentaire