HI all,
I’m in need of some design advice here.
I have a hierarchy of classes representing logical formulas. I then
have some visitors that do stuff on them.
One of those visitors has the general method
public void visit(Formula form){
// do some pre-visit work on the formula, such as bringing it in
normal form
form.accept(this)
// do some post-visit work on the result, such as make sure the
variables are singletons where need be
}
Then, of course, there are all the other methods for all possible
subclasses of the Formula interface:
public void visit(Negation form) {
// do stuff specific to negation
setResult(whateverComputed);
}
and similar for Disjunction, Inclusion, ...
Now, the problem is: if a user declares a formula of type Negation, and
then lets the builder visit it:
Negation neg = new Negation(negatedFormula);
builder.visit(neg);
it will of course jump into the formula for Negation. That should not
happen, since the computations done before and after visiting are
necessary (e.g. visit(Implication) just throws an exception, since they
do not occur in the normal form).
Is there a way to prevent users from calling visit(Negation), to force
them to use visit(Formula)?
A sketch of the current design is:
public interface Visitable {
void accept(final FormulaVisitor visitor) throws VisitorException;
}
public interface Formula extends Visitable {
// stuff common to formulas
}
public interface FormulaVisitor {
void visit(final Formula form) throws VisitorException;
void visit(final Negation form) throws VisitorException;
void visit(final Conjunction form) throws VisitorException;
... for other subclasses
}
Aha, typing this out gave me the solution: I can make FormulaVisitor an
abstract class and all but the visit(Formula) methods protected. That
solves the problem.
However, if someone sees a better way, please tell me!
Cheers, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green - 13 Jul 2007 16:40 GMT
On Fri, 13 Jul 2007 14:49:38 +0200, Hendrik Maryns
<hendrik_maryns@despammed.com> wrote, quoted or indirectly quoted
someone who said :
>Is there a way to prevent users from calling visit(Negation), to force
>them to use visit(Formula)?
This is not intended as a suggested finished solution, but merely to
spark a train of thought. What would happen if you encapsulated your
distinction using generics?
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Hendrik Maryns - 16 Jul 2007 12:45 GMT
Roedy Green schreef:
> On Fri, 13 Jul 2007 14:49:38 +0200, Hendrik Maryns
> <hendrik_maryns@despammed.com> wrote, quoted or indirectly quoted
[quoted text clipped - 6 lines]
> spark a train of thought. What would happen if you encapsulated your
> distinction using generics?
I don’t really see how that would work. You mean make Visitor generic?
That would be about the same solution as I suggested, since the only
meaningful generic parameter would be Formula, and thus only one method
visit(Formula) could be defined. That is how I did it now, making all
other methods protected (in the abstract superclass FormulaVisitor). So
I sort of have an extension of your idea. Less general, acknowledged.
Thanks for your input.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green - 13 Jul 2007 16:45 GMT
On Fri, 13 Jul 2007 14:49:38 +0200, Hendrik Maryns
<hendrik_maryns@despammed.com> wrote, quoted or indirectly quoted
someone who said :
>Is there a way to prevent users from calling visit(Negation), to force
>them to use visit(Formula)?
It comes out in the wash if Formula is a subclass of Negation. Java
automatically selects them most specific variant at compile time.
(Nice does it at run time).
Perhaps using (dummy) interfaces that are subclasses of each other
that your various classes implement, you can force the desired
behaviours.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Hendrik Maryns - 16 Jul 2007 12:42 GMT
Roedy Green schreef:
> On Fri, 13 Jul 2007 14:49:38 +0200, Hendrik Maryns
> <hendrik_maryns@despammed.com> wrote, quoted or indirectly quoted
[quoted text clipped - 10 lines]
> that your various classes implement, you can force the desired
> behaviours.
That sounds awful. Surely, I don’t want Formula to inherit from
Negation?? Inheritance still has something to do with is-a right?
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html