Re: Prolog-style pattern matching in Java. How to?
On Aug 26, 11:01 pm, Patricia Shanahan <p...@acm.org> wrote:
Hunter Gratzner wrote:
On Aug 26, 10:10 pm, Patricia Shanahan <p...@acm.org> wrote:
Apply them in a loop:
for(Matcher m: allMatchers){
if(m.matches(whatever parameters)){
// Deal with a hit
Better, let the "matcher" deal with the hit => Visitor
interface Visitor {
public void visit(Computation c);
}
Visitor example1 = new Visitor() {
public void visit(Computation c) {
if(c.getA() > 5 && "".equals(c.getT()) && c.getP() != null) {
// manipulate Computation c in some way
// c.setX(...); c.setY(...);
}
}
}
I would make the choice between the two approaches depending on how the
reaction to a match depends on the pattern. If there is a fixed
reaction, then the matcher should just decide whether there is a match.
If a match condition and a reaction to it are logically coupled, then
the visitor pattern is better.
Patricia
Thanks again for your helpful and constructive answers. I'll let them
sink in.
Ulrich