Re: Problem applying generics to my code. Is there a better solution?
- From: "Daniel Pitts" <googlegroupie@xxxxxxxxxxxxx>
- Date: 5 Apr 2007 14:55:28 -0700
On Apr 5, 1:50 am, "Chris Uppal" <chris.up...@xxxxxxxxxxxxxxxxxxx
THIS.org> wrote:
Daniel Pitts wrote:
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.
Small point, but "Visitor pattern" is an application of the broadly useful
technique of double-dispatch (which can be called a pattern too, if you like,
though it isn't often seen that way). In this case, I think, your suggestion
would be better seen as a direct application of double-dispatch, rather than an
application of Visitor -- and a better name could be found for the visit()
method (exposing what it /does/ rather than what feature of "Visitor" it
reflects).
A suggestion for the OP. Design and implement this stuff without using
generics at all, then go back and put in the necessary generic declarations.
Double dispatch -- or whatever -- is part of the /semantics/ of your system,
whereas generics are just a way of telling the typechecker what rules correct
applications of those semantics will follow. So unless generics make the
design/code easier to articulate in the first place (which I doubt in this
particular case), trying to keep the typechecker happy at the same time as
trying to think through the actual object interactions will be
counter-productive.
-- chris
Thanks Chris, I hadn't heard of the double dispatch pattern before,
and it is what I've demonstrated.
On Apr 5, 12:04 am, "Lucas" <lscha...@xxxxxxxxx> wrote:
public static <T, P> Distribution<P> conjugate( List<T> data,If I'm understanding you correctly
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.
public static <T, P extends Distribution<?>> P conjugate(List<T>
data, Distribution<T> likelihood, P prior);
Now, conjugate sounds like a verb. Are you conjugating a likelihood
or a prior? Does it make sense to make the conjugate method into the
likelihood or prior object instead?
Which one of these three makes more sense?
"Likelihood, conjugate using data and prior":
likelihood.conjugate(data, prior)
or
"Prior, conjugate using data and likelihood":
prior.conjugate(data, likelihood)
or (where Data isn't just a List<T>)
"Data, conjugate using likelihood and prior):
data.conjugate(likelihood, prior)?
If you find that the last one makes the most sense, you might decide
to "wrap" a List<T> into a Data<T> object which implements the method
conjugate. You might want to wrap the List<T> into a Data class
anyway, depending on other uses.
Hope this helps even more :-)
Daniel
.
- Follow-Ups:
- 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
- Re: Problem applying generics to my code. Is there a better solution?
- From: Chris Uppal
- Problem applying generics to my code. Is there a better solution?
- Prev by Date: Re: For Loops and Variables
- Next by Date: Re: Wierd ArrayIndexOutOfBounds
- 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
|