I’m writing some scientific code that generates / imports some data (let’s call that X) to generate other set of data (we can call them Y). The problem is there more than one way to generate X and this generation method affects the calculation formula of Y.
I’ve developed a library that handles the data import and calculation stuff but, I’m unable to decide a sound mechanism for modifying the formula without user intervention.
What’s the best way to handle such cases? I’ve implemented different functions for different generation methods for X, but calculation method for Y is single.
To clarify more, I want to implement the following behavior:
- User calls one of the import functions that generates / imports X,
- The magic happens and the formula calculating Y is updated somehow.
- User then calls calc() function that generates Y.
All functions mentioned in this questions are concrete, simple and dumb in-class functions.
I’m not sure how to implements the trigger mechanism inside the functions that generate / import X and make the magic work. The method for storing the formulations are answered by helpful fellows @UnknownGosu and @Dima-Rudnik.
Thanks in advance.
You need a form of the strategy pattern.
Just like @UnknownGosu pointed out. Define an abstract
Formulainterface, something like this:Than create a concrete
FormulaEdit:
In procedural programming, the same is accomplished with function pointers.
For example, an implementation of QuickSort that takes a pointer to a sorting function.