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());
question from:
https://stackoverflow.com/questions/65649152/design-pattern-for-executing-various-operations-depending-on-flags-passed-as-arg 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…