public Staff(String name) { this.name = name; this.kpi = new Random().nextInt(10*10000); }
public abstract void accept(Visitor visitor);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public class Engineer extends Staff {
public Engineer(String name) { super(name); }
@Override public void accept(Visitor visitor) { visitor.visit(this); }
public int getCodeLines(){ return new Random().nextInt(10*10000); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class Manager extends Staff {
public Manager(String name) { super(name); }
@Override public void accept(Visitor visitor) { visitor.visit(this); }
public int getProducts(){ return new Random().nextInt(10); }
}
1 2 3 4
public interface Visitor { public void visit(Engineer engineer); public void visit(Manager manager); }
1 2 3 4 5 6 7 8 9 10 11
public class CEOVisitor implements Visitor { @Override public void visit(Engineer engineer) { System.out.println("工程师:"+engineer.name+",KPI:"+engineer.kpi); }
@Override public void visit(Manager manager) { System.out.println("经理:"+manager.name+",KPI:"+manager.kpi+",新产品数量:"+manager.getProducts()); } }
1 2 3 4 5 6 7 8 9 10 11
public class CTOVisitor implements Visitor { @Override public void visit(Engineer engineer) { System.out.println("工程师:"+engineer.name+",代码行数:"+engineer.getCodeLines()); }
@Override public void visit(Manager manager) { System.out.println("经理:"+manager.name+",产品数量:"+manager.getProducts()); } }