// behavior // traversal is from A to X // visitors are called in the order A, B, X A { (@ void traverse(AbstractVisitor v) { v.visit_A(this); b.traverse(v); } @) } B { (@ void traverse(AbstractVisitor v) { v.visit_B(this); } @) } X { (@ void traverse(AbstractVisitor v) { super.traverse(v); v.visit_X(this); } @) } AbstractVisitor { (@ void visit_A(A host) {} void visit_B(B host) {} void visit_X(X host) {} @) } ConcreteVisitor { (@ void visit_A(A host) { this.weighted_sum(2); } void visit_B(B host) { this.weighted_sum(3); } void visit_X(X host) { this.weighted_sum(4); } void weighted_sum(int w) { set_c(new Integer(c.intValue() + c.intValue() * w )); } @) } Main { (@ static public void main(String args[]) throws Exception { A a; a = A.parse(System.in); ConcreteVisitor t = ConcreteVisitor.parse("1"); a.traverse(t); int result = t.get_c().intValue(); System.out.println("count " + result); if (result == 60) System.out.println("SUCCESS"); else System.out.println("FAILURE"); } @) }