Re: implementation dilema
- From: Ed Kirwan <iamfractal@xxxxxxxxxxx>
- Date: Fri, 31 Mar 2006 14:51:22 +0200
ma740988@xxxxxxxxx wrote:
Ed Kirwan wrote:ma740988@xxxxxxxxx wrote:[ snip ]
Think: variance encapsulation.That's pretty awesome. This at least gets me on the right track.
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);
}
Should AlgorithmSequence be a factory or is it AlgorithmInterface?
Well, I suppose what I wrote up above and what I showed in the pseudo code are different - sorry for the confusion. I was initially thinking of having a factory method pattern somewhere, whereby you'd pass in a parameter and get back an Algorithm interface behind which was the algorithm implementation that corresponds to the parameter, but I ended up not putting this in the pseudo code; so code as it stands doesn't have the factory pattern I'd thought about.
If it had been included, then the Configuration class would change as follows (though I don't think this is necessary, just maybe of interest to know; a factory is only needed if the algorithm implementations are instantiated from more than one place; at the moment, this instantiation is only done in Configuration, but if you want them elsewhere, then the factory would be a good idea):
class Configuration {
void start() {
AlgorithmSequence algorithmSequence = new AlgorithmSequence();
AlgorithmFactory factory = new AlgorithmFactory();
algorithmSequence.add(factory.getAlgorithm(MEAN_ALGORITHM));
algorithmSequence.add(factory.getAlgorithm(DEVIATION_ALGORITHM));
algorithmSequence.add(factory.getAlgorithm(FFT_ALGORITM));
}
}
And the factory method itself would be pretty standard:
class AlgorithmFactory {
Algorithm getAlgorithm(int parameter) {
switch (parameter) {
case MEAN_ALGORITHM:
return new new MeanAlgorithm();
case DEVIATION_ALGORITHM:
return new new DeviationAlgorithm();
case FFT_ALGORITHM:
return new new FFTAlgorithm();
}
}
}
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Download Fractality, free Java code analyzer:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html
.
- References:
- implementation dilema
- From: ma740988
- Re: implementation dilema
- From: Ed Kirwan
- Re: implementation dilema
- From: ma740988
- implementation dilema
- Prev by Date: Re: implementation dilema
- Next by Date: Re: implementation dilema
- Previous by thread: Re: implementation dilema
- Next by thread: Re: implementation dilema
- Index(es):
Relevant Pages
|