我的一個visitor實作

VRGL發表於2003-04-05

public interface Visitable
{
void accept(Visitor visitor);
}

public interface Visitor
{
void visit(Visitable visitable);
}


public class VisitableA implements Visitable
{
public String Call(){
return "AAA";
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}



public class VisitorA implements Visitor
{ public void visit(Visitable visitable) {
if (visitable instanceof VisitableA)
{
System.out.println(((VisitableA)visitable).Call());
}
}

}


public class Client{
public static void main(String args[])
{
Visitor visitor=new VisitorA();
Visitable visitable=new VisitableA();
visitable.accept(visitor);
}
}

相關文章