Re: on a design question using C++
On Feb 6, 11:06 am, "nvinh...@gmail.com" <nvinh...@gmail.com> wrote:
On Feb 5, 1:59 pm, Michael DOUBEZ <michael.dou...@free.fr> wrote:
[...]
So, you propose to use the so-called template method pattern
which uses orthogonal classes as policy classes and a template
class SofteningLaw.
I actually proposed several possibilities. No one is ideal in
all circumstances.
It seems to me very great but my softening law is defined at
run time. I only know write code like this:
typedef SofteningLaw <LinearSofteningPolicy> LinearSofteningLaw;
typedef SofteningLaw <ExponeSofteningPolicy> ExponeSofteningLaw;
LinearSofteningLaw softLaw;
double f = softLaw.computeVal( 3.0 );
double d = softLaw.computeDer( 3.0 );
However, how to write code in such a way that softLaw is defined
correctly at run time?
I'm not sure what you mean by "defined at runtime". If you mean
that the choice is only made at runtime, then you can use the
classical variants (without templates) of either the template
method pattern or the strategy pattern---the strategy pattern
will even allow changing the softLaw on an existing object. If
you mean that the actual law will only be defined at runtime (as
opposed to choosing between a set of existing laws), it becomes
more complicated. If the definition only involves choosing
among a set of possible algorithms and inserting parameters,
then your still basically within what the strategy pattern, or
even the template method pattern, can do. If it is a question
of reading some formula or equations from a configuration file
or other user input, and using that as the softLaw, you'll need
some sort of parser.
--
James Kanze