Re: implementation dilema
- From: Ed Kirwan <iamfractal@xxxxxxxxxxx>
- Date: Fri, 31 Mar 2006 11:10:41 +0200
ma740988@xxxxxxxxx wrote:
Assume a system with three cards. For discussion purposes we'll call
them A B and C. Assume data comes in from an external source to A & B.
A and B computes mean on data and transmits result to C. C performs a
sum on the mean of A and B and transmits _its_ (C's result) result to A
and B. A and B runs an _algorithm_ on the data utilizing the result
from C. A and B sends result from the algorithm to C. A, B and C play
this 'game' (i.e A and B does something send results to C, C does
something send result to A and B and so on ) for say 7 iterations.
Assume pictorially this is akin to:
A & B C
---------------------------------------------------------
mean mean
standard deviation standard deviation
algorithm_ab1 algorithm_c1
algorithm_ab2 algorithm_c2
algorithm_ab3 algorithm_c3
algorithm_ab4 algorithm_c4
algorithm_ab5 algorithm_c5
The question: I'm trying to design a class that'll capture the fact
that the mean and standard deviation are common to 'both' ( code on A
and B is generic, so both here is A/B and C). From there the
algorithms are different.. I'm not sure if I'm missing a design
pattern or .. what's a good way to architect this?
One other thing. Some of the algorithms on both sides is comprised of
FFTs. For instance ( algorithm_ab1/ab2/ab4 is comprised of an FFT,
similarily algorithm_c2/c3/c4 is comprised of an FFT).
Not sure how to put this together nicely to make maintenance easy.
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);
}
Of course, this presumes a uniform interface towards the data.
Actually, it presumes quite a lot ...
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Download Fractality, free Java code analyzer:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html
.
- Follow-Ups:
- Re: implementation dilema
- From: ma740988
- Re: implementation dilema
- References:
- implementation dilema
- From: ma740988
- implementation dilema
- Prev by Date: Re: RFC - New Object-Oriented Method of Parallel Programming
- Next by Date: Re: implementation dilema
- Previous by thread: implementation dilema
- Next by thread: Re: implementation dilema
- Index(es):
Relevant Pages
|