package study.org;
public class Demo {
public static void main(String[] args) {
Visitor visitor = new MyVisitor();
Subject sub = new MySubject();
sub.accept(visitor);
}
}
interface Visitor {
public void visit(Subject sub);
}
interface Subject {
public void accept(Visitor visitor);
public String getProperty();
}
class MyVisitor implements Visitor {
@Override
public void visit(Subject sub) {
System.out.println(sub.getProperty());
}
}
class MySubject implements Subject {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
@Override
public String getProperty() {
return "property value";
}
}
网友评论