I would like to make a class "A" which contains other classes "A_contained_1" and "A_contained_2" (they may also contain other classes, e.g., like "A_contained_1" contains "A_contained_1_contained"). Lets say each of the contained classes "A_contained_1" and "A_contained_2" have several methods, which for every method you may have two or more formula (F1 and F2) which you want to implement and they do exactly the same thing. Once you implemented all the classes with their methods, you want to instantiate an object "A" which its "A_contained_1" runs with F1 implementation and another object "A" which its "A_contained_1" runs with F2 implementation and so on. You clearly see that the number of possibilities can be very large.
An example of such a system but a very simple one is given below:
Problem: Consider I have a class "Calculator" which contains class "Engine". Engine has a function to generate two numbers "a" and "b". We want the possibility to generate number with two different formula, "method_1" and "method_2". "Calculator" has a method called "Multiply" which multiplies the numbers "a" and "b" generated by "Engine". Now by the choice of the user I want to make a calculator which the numbers are generated by "method_1" or "method_2" of the engine, How do I design the code?? I really want to avoid passing flags !
Solution: I define a class "engine" and extend it to two classes "engine_1" with "method_1" implememtation and and "engine_2" with "method_2" implementation. Then I define a class calculator which has polymmorphic pointer to "class (engine)". Based on the choice of user, I define an engine of "engine_1" or "engine_2" and point the pointer to that. Therefore, my calcuator uses the apprpriate method based on the user selection! Please see the simple attached code!
pros: With this approach I do not need to pass some sort of flag every time I call the function of the calculator, so I may save some run time.
cons: What If I have another class "student" which contains calculator and becuase of some variety of the formula in calculator I need to also make several calculators? then with a similar approach I need to define the student with a pointer to calculator which makes things crazy in my opinion. Also, if I have 10 different formula in engine and 10 other in calculator, which there are just slightly different in one part, I need to extend engine and calcuator 10 times each which is kind of tedious!!
Although I tried to resolve this issue myself I think I am not being very sucessfull. So any Idea how to design a code like this example in a more elegant way?
Thank you very much for your inputs