Reputation: 65
For purposes of learning new technology, I want to create simple utility app that generates mathematical statistics. There are various operations that the utility can do, lets say for now there are 3 of them:
utility.sum(); utility.geometricMean(); utility.arithmeticMean();
User can request generating 3 statistics from above, depending on flags he specifies when executing app, let's say
-s -gm -am
The point is user can select any combination of N statistics, so when he runs app with -s -am
flags, the app should execute utility.sum(); utility.arithmeticMean();
methods.
I am wondering, what would be an elegant design (probably using a design pattern?) of such module, that corresponds to all clean code, clean design and SOLID principles.
The flags can be represented in a way that suits the solution best - booleans, integers, enums - it does not really matter for me.
EDIT: As MrFisherman suggested, it is a good idea to implement the mathematical operations itself with Command, but im mostly concerned about HOW TO CREATE those commands depending on flags passed. Is there any way to avoid doing stuff like below?
if(flag == '-am') commands.add(new ArithmeticMeanCommand());
else if(flag == '-gm') commands.add(new GeometricMeanCommand());
else if(flag == '-s') commands.add(new CalculateSumCommand());
Upvotes: 0
Views: 789
Reputation: 738
I think you can use Command: https://en.wikipedia.org/wiki/Command_pattern and put every action in separate class like geometricMeanCommand, arithmeticMeanCommand etc. But in your problem as our friend who delete his answer said, you can just use simple switch statement. You can also use Facade design pattern https://en.wikipedia.org/wiki/Facade_pattern and share just a couple of methods to your main method (inside you will be able to combine them in different ways).
Upvotes: 2
Reputation: 50
I think a simple switch statement could do the trick here. In the arguement you can pass in the flags and the cases map to the method you would like to invoke.
Upvotes: 0