Visitor pattern: forcing to start with most general method?

H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGl3Tie+7xMGD3itQRAq0/AJ9ZnA4G6YiLyC3zMTAPwuw5RpYyLgCeMYLr
JtQM6hgFmzQzQ0X5qmPokn8=
=0IX8
-----END PGP SIGNATURE-----
 
R

Roedy Green

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?
 
R

Roedy Green

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.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Roedy Green schreef:
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.

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGm1m0e+7xMGD3itQRAj7bAJ91RMymPtCOkqTQKomfdFU0Yd/LkQCggKs7
lkjBIJqSecjdWeidl/hrPE4=
=ST9T
-----END PGP SIGNATURE-----
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Roedy Green schreef:
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?

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGm1pQe+7xMGD3itQRAoUaAJ93eL6w8J+pABoz4Q/OHP5EVb/uDQCfVs9b
4Asa25Nr/gX2eDktc3RQvEI=
=HDP0
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top