首页 > 代码库 > 设计模式_訪问者模式
设计模式_訪问者模式
Repressent an operation to be performed on the elements of an object structure.Visitor lets you define a new operation without changing the classees of the elements on which it operates.(封装一些作用于某种数据结构中的各种元素的操作,它能够在不改变数据结构的前提下定义作用于这些元素的新操作。)
直接上代码吧,心态不佳。
public abstract class Employee { public final static int MALE=0; public final static int FEMALE=1; private String name; private String salary; private int sex; public abstract void accept(IVisitor visitor); //allow a visitor public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } }
public class Manager extends Employee{ private String performance; public String getPerformance() { return performance; } public void setPerformance(String performance) { this.performance = performance; } @Override public void accept(IVisitor visitor) { visitor.visit(this); } }
public class CommonEmployee extends Employee{ private String job; public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Override public void accept(IVisitor visitor) { visitor.visit(this); } }
public interface IVisitor { public void visit(CommonEmployee commonEmployee); public void visit(Manager manager); }真正的处理类在这里。
public class Visitor implements IVisitor { public void visit(CommonEmployee commonEmployee) { System.out.println(this.getCommonEmployee(commonEmployee)); } public void visit(Manager manager) { System.out.println(this.getManagerInfo(manager)); } private String getBasicInfo(Employee employee) { String info = "name:" + employee.getName() + "\t sex:" + (employee.getSex() == Employee.FEMALE ?"man" : "women") + "\t salary:" + employee.getSalary(); return info; } private String getManagerInfo(Manager manager) { String basic = this.getBasicInfo(manager); String otherInfo = "\t performance" + manager.getPerformance(); return basic + otherInfo; } private String getCommonEmployee(CommonEmployee commonEmployee){ String basic=this.getBasicInfo(commonEmployee)+"\t"; String other="job:"+commonEmployee.getJob(); return basic+other; } }
測试
public class Client { public static void main(String[] args) { for(Employee e:mockEmployee()){ e.accept(new Visitor()); } } public static List<Employee> mockEmployee(){ List<Employee> list=new ArrayList<Employee>(); CommonEmployee zhangsan=new CommonEmployee(); zhangsan.setJob("coding everyday!"); zhangsan.setName("zhangsan"); zhangsan.setSalary("1800"); zhangsan.setSex(Employee.MALE); CommonEmployee lirong=new CommonEmployee(); lirong.setJob("page coding,It‘s not fansion"); lirong.setName("lirong"); lirong.setSalary("1900"); lirong.setSex(Employee.FEMALE); Manager wang=new Manager(); wang.setName("wamg"); wang.setPerformance("It‘s unpay basically,but he is a horser!"); wang.setSalary("10221"); wang.setSex(Employee.MALE); list.add(zhangsan); list.add(lirong); list.add(wang); return list; } }
public class Visitor implements IVisitor { //部门经理的工资系数是5 private final static int MANAGER_COEFFICIENT = 5; //员工的工资系数是2 private final static int COMMONEMPLOYEE_COEFFICIENT = 2; //普通员工的工资总和 private int commonTotalSalary = 0; //部门经理的工资总和 private int managerTotalSalary =0; //计算部门经理的工资总和 private void calManagerSalary(int salary){ this.managerTotalSalary = this.managerTotalSalary + salary *MANAGER_COEFFICIENT ; } //计算普通员工的工资总和 private void calCommonSlary(int salary){ this.commonTotalSalary = this.commonTotalSalary + salary*COMMONEMPLOYEE_COEFFICIENT; } //获得全部员工的工资总和 public int getTotalSalary(){ return this.commonTotalSalary + this.managerTotalSalary; } }
我是菜鸟,我在路上。
设计模式_訪问者模式