Re: implementation dilema




Ed Kirwan wrote:
ma740988@xxxxxxxxx wrote:
[ snip ]

Think: variance encapsulation.

Ask yourself, "What part of this design is changing?" Or more generally,
"What concept is varying and how can I encapsulate it?"

The variance would appear to be: the algorithms working on the
data, and the data themselves. (So, pretty much the entire thing,
then.)

Rather than seeing A, B, and C, you should probably have an Algorithm
interface and a Data interface.

The Algorithm interface will be implemented by all the various
alorithms that you use, for example, you'll have a MeanAlgorithm,
StandardDeviationAlgoirthm, FFTAlgorithm, etc. This way, if both
MeanAlgorithm and StandardDeviationAlgoirthm share common behaviour,
then they can inherit from some CommonMeanAlgorithm: all of this will
be hidden to clients, who just see an Algorithm interface. This
strongly suggests a strategy pattern and some sort of factory pattern.

For flexibility, don't hard-code the sequence in
which the algorithms are executed in the algorithms themselves. Instead,
you could consider having an AlgorithmSequence, in which is configured
at start-up with the algorithms and sequence that you want; you'll thus
be able to change this easily as your needs evolve.

So, pseudo-code:

class Configuration {

void start() {
AlgorithmSequence algorithmSequence = new AlgorithmSequence();
algorithmSequence.add(new MeanAlgorithm());
algorithmSequence.add(new StandardDeviationAlgoirthm());
algorithmSequence.add(new FFTAlgorithm());
}
}

class CalculationRun {

void calculate(Data data) {
Configuration configuration = getConfiguration();
AlgorithmSequence algorithmSequence = configuration.getAlgSeq();
int numAlgorithms = algorithmSequence.getNum();
for (int i = 0; i < numAlgorithms; i++) {
Algorithm alg = algorithmSequence.getAlg(i);
data = alg.execute(data);
}
System.out.println("Answer = " + data);
}
That's pretty awesome. This at least gets me on the right track.
Should AlgorithmSequence be a factory or is it AlgorithmInterface?

.