Re: Problem applying generics to my code. Is there a better solution?
- From: "Lucas" <lscharen@xxxxxxxxx>
- Date: 5 Apr 2007 00:04:10 -0700
Anytime that you find yourself using a chain of "instanceof" checks on
classes which you control, its time to look into using polymorphism
instead... You're particular problem may be best solved through a
Visitor pattern.
Yet another reason for me to get my own copy of the GoF.
Here's an example, I'm removing the generics, as they're not necessary
for my point.
I'll comment on your suggestion below, but I wanted to re-emphasize
the point that I really would like to use generics for the their type-
safety in this case. Recall the function prototype I mentioned in the
OP
public static <T, P> Distribution<P> conjugate( List<T> data,
Distribution<T> likelihood, Distribution<P> prior )
Fro, the perspective of enforcing mathematical correctness, it is
important to constrain the return type to be the same as the prior
parameters and force the data to be compatible with the likelihood.
Now, on to your (slightly edited) suggestion....
abstract class Distribution {
Distribution posterior(List list, Distribution prior) {
prior.visit(this);
}
protected Distribution posterior(List list, Normal prior) {
throw new IllegalArgumentException("Not valid");
}
protected Distribution posterior(List list, Gamma prior) {
throw new IllegalArgumentException("Not valid");
}
public abstract Distribution visit(List list, Distribution d);}
Ok, so we set up a base class (abstract in this example) where we
consider every possible distribution as a potential prior. It will be
up to the implementing classes to override the conjugate cases
supported by their particular form.
class Normal {
protected Distribution posterior(List list, Normal prior) {
return normalNormaPosterior(list, this, prior);
}
protected Distribution posterior(List list, Gamma prior) {
return normalGammaPosterior(list, this, prior);
}
public Distribution visit(List list, Distribution d) {
return d.posterior(list, this);
}
}
Now, when we invoke .posterior(data, prior) for a distribution x, it
will visit the prior, passing itself along as a parameters. Since we
invoke .visit() on the prior object, it will call the correct method,
i.e. normal or gamma or whatever. Now, the prior calls back to the
likelihood object, passing itself. This will give the type
specificity to actually call one of the polymorphic posterior()
methods. Very nice!! This is a double-dispatch pattern, correct?
I actually don't know much about the problem domain your solving. I'm
guessing statistics? (The one math I haven't studied much) :-)
Correct. The particular bit of statistics I'm implementing here is
described at
http://en.wikipedia.org/wiki/Conjugate_prior
Anyway, I hope I've been able to help.
Very much! I'll give your suggestion a whirl and see how it plays
out.
Many thanks.
.
- References:
- Problem applying generics to my code. Is there a better solution?
- From: Lucas
- Re: Problem applying generics to my code. Is there a better solution?
- From: Daniel Pitts
- Re: Problem applying generics to my code. Is there a better solution?
- From: Lucas
- Re: Problem applying generics to my code. Is there a better solution?
- From: Daniel Pitts
- Problem applying generics to my code. Is there a better solution?
- Prev by Date: Q: Constructors Polymirphisms
- Next by Date: Eclipse: how to set breakpoint on library JAR source?
- Previous by thread: Re: Problem applying generics to my code. Is there a better solution?
- Next by thread: Re: Problem applying generics to my code. Is there a better solution?
- Index(es):
Relevant Pages
|