<!--StartFragment-->
<!--StartFragment-->
抽象访问者(Visitor): 抽象类或者接口, 声明访问者可以访问哪些元素, 具体到程序中就是 visit方法的参数定义哪些对象是可以被访问的;
package VisitorPattern.visitor; import VisitorPattern.element.CommonEmployee; import VisitorPattern.element.ManagerEmployee; //申明可以访问那些元素 public interface Visitor { public void visit(CommonEmployee employee); public void visit(ManagerEmployee employee); }
package VisitorPattern.visitor; public interface ShowVisitor extends Visitor{ public String show(); }
package VisitorPattern.visitor; public interface TongjiVisitor extends Visitor{ public int getTotalSalary(); }
<!--StartFragment-->
具体访问者(ConcreteVisitor): 访问者访问到一个类后该怎么干(哎, 这个别读歪了), 要做什么事情;
package VisitorPattern.visitor; import VisitorPattern.element.CommonEmployee; import VisitorPattern.element.ManagerEmployee; public class ShowVisitorImpl implements ShowVisitor{ private String info = ""; @Override public void visit(CommonEmployee employee) { info = info + "只关心普通员工的工作:"+employee.getJob()+";"; } @Override public void visit(ManagerEmployee employee) { info = info + "只关心经理的姓名:"+employee.getName()+";"; } @Override public String show() { return info; } }
package VisitorPattern.visitor; import VisitorPattern.element.CommonEmployee; import VisitorPattern.element.ManagerEmployee; //用作统计 public class TongjiVisitorImpl implements TongjiVisitor{ private int commonEmTotalSalary; private int managerEmTotalSalary; @Override public void visit(CommonEmployee employee) { commonEmTotalSalary = commonEmTotalSalary+employee.getSalary(); } @Override public void visit(ManagerEmployee employee) { managerEmTotalSalary = managerEmTotalSalary + employee.getSalary(); } public int getTotalSalary(){ return commonEmTotalSalary + managerEmTotalSalary; } }
<!--StartFragment-->
抽象元素(Element): 接口或者抽象类, 声明接受那一类型的访问者访问, 程序上是通过 accept 方法
中的参数来定义;
<!--EndFragment-->
package VisitorPattern.element; import VisitorPattern.visitor.Visitor; public abstract class Employee { private String name; private String job; private int salary; public Employee(String name,String job,int salary){ this.name = name; this.job = job; this.salary = salary; } public abstract void accept(Visitor visitor); public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } }
<!--StartFragment-->
具体元素:(ConcreteElement): 实现 accept 方法, 通常是 visitor.visit(this), 基本上都形成了一
个套路了;
<!--EndFragment-->
package VisitorPattern.element; import VisitorPattern.visitor.Visitor; public class CommonEmployee extends Employee{ public CommonEmployee(String name, String job,int salary) { super(name, job,salary); } @Override public void accept(Visitor visitor) { visitor.visit(this); } }
package VisitorPattern.element; import VisitorPattern.visitor.Visitor; public class ManagerEmployee extends Employee{ public ManagerEmployee(String name, String job,int salary) { super(name, job,salary); } @Override public void accept(Visitor visitor) { visitor.visit(this); } }
<!--StartFragment-->
结构对象(ObjectStruture): 容纳多个不同类、 不同接口的容器, 比如 List、 Set、 Map 等, 在项目中,
一般很少抽象出来这个角色;
<!--EndFragment-->
客户端调用
package VisitorPattern; import java.util.ArrayList; import java.util.List; import VisitorPattern.element.CommonEmployee; import VisitorPattern.element.Employee; import VisitorPattern.element.ManagerEmployee; import VisitorPattern.visitor.ShowVisitor; import VisitorPattern.visitor.TongjiVisitor; import VisitorPattern.visitor.TongjiVisitorImpl; import VisitorPattern.visitor.ShowVisitorImpl; public class Client { /** * @param args */ public static void main(String[] args) { ShowVisitor showVisitor = new ShowVisitorImpl(); TongjiVisitor tongjiVisitor = new TongjiVisitorImpl(); List<Employee> emList = new ArrayList<Employee>(); emList.add(new CommonEmployee("张三","美工",2500)); emList.add(new ManagerEmployee("王五","财务经理",7000)); emList.add(new CommonEmployee("李四","架构师",6000)); emList.add(new ManagerEmployee("赵六","IT经理",8000)); for (Employee employee : emList) { employee.accept(showVisitor); employee.accept(tongjiVisitor); } System.out.println(tongjiVisitor.getTotalSalary()); System.out.println(showVisitor.show()); } }
<!--EndFragment--><!--EndFragment-->