package com.test.design.visitor;
public abstract class Employee {
private String name;
private int salary;
private String hoby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getHoby() {
return hoby;
}
public void setHoby(String hoby) {
this.hoby = hoby;
}
public abstract void acceptVisitor(IVisitor visitor);
}
package com.test.design.visitor;
public class CommonEmployee extends Employee {
@Override
public void acceptVisitor(IVisitor visitor) {
visitor.visit(this);
}
}
package com.test.design.visitor;
public class ManamgerEmployee extends Employee {
@Override
public void acceptVisitor(IVisitor visitor) {
visitor.visit(this);
}
}
package com.test.design.visitor;
public interface IVisitor {
public void visit(CommonEmployee commonEmployee);
public void visit(ManamgerEmployee manamgerEmployee);
}
package com.test.design.visitor;
public class Visitor implements IVisitor {
@Override
public void visit(CommonEmployee commonEmployee) {
//调用CommonEmployee中的各种方法
}
@Override
public void visit(ManamgerEmployee manamgerEmployee) {
//调用ManamgerEmployee中的各种方法
}
}
package com.test.design.visitor;
import java.util.ArrayList;
public class Client {
public static void main(String[] args) {
IVisitor visitor = new Visitor();
for(Employee employee : generate()){
employee.acceptVisitor(visitor);
}
}
private static ArrayList<Employee> generate(){
ArrayList<Employee> employees = new ArrayList<Employee>();
CommonEmployee commonEmployee = new CommonEmployee();
ManamgerEmployee manamgerEmployee = new ManamgerEmployee();
employees.add(commonEmployee);
employees.add(manamgerEmployee);
return employees;
}
}