I am working on an application that will shortly be used for provisioning. The situation is not really complicated. The application manages groups that have access to services. Depending of the services the group has access, different business rules are applied.
The first idea (simple and stupid) would be to have a big if/else or switch/case that will cover all the cases. Here is how it could look like in pseudocode :
if (serviceA OR serviceB) {
doActionA();
}
if(serviceA AND serviceC) {
doActionA();
doActionB();
}
if(serviceB AND serviceC) {
doActionA();
doActionC();
}
if(serviceA AND serviceB AND serviceC) {
doActionA();
doActionB();
doActionC();
}
if(serviceD) {
doActionD();
}
It is not very convenient because if add a new service, I would need to update this statement (that could be hundreds/thousands of lines long ! ).
After realizing that, I made my researches about which design pattern could help me solve this problem. The one that looks like could help me would be the Strategy pattern. Again, it doesn't change anything (or I don't understand it) and the code would look like this :
if (serviceA OR serviceB) {
setStrategy(new StrategyA);
}
if(serviceA AND serviceC) {
setStrategy(new StrategyB);
}
if(serviceB AND serviceC) {
setStrategy(new StrategyC);
}
if(serviceA AND serviceB AND serviceC) {
setStrategy(new StrategyD);
}
if(serviceD) {
setStrategy(new StrategyE);
}
strategy.run();
In a way, this is even worse because I can only have one strategy, and notice how the serviceD does not have business rules related to others (it just have an action for itself). Then again, maybe i didn't understood the pattern.
Do you have any idea how I can handle such situation in the most elegant way ? I know that in the future I will have new services so I don't want to make a mistake now. I hope I don't need to write a strategy for every combination (7 strategies for 3 services, 14 strategies for 4 services, it goes fast !)
Thank you by advance for your help :)
Aucun commentaire:
Enregistrer un commentaire