设计模式_访问者模式

来源:互联网 发布:巨人网络收购alpha 编辑:程序博客网 时间:2024/06/14 21:17
Visitor Pattern 

   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; }}

优点 
● 符合单一职责原则
● 优秀的扩展性
● 灵活性非常高

缺点
● 具体元素对访问者公布细节
● 具体元素变更比较困难
● 违背了依赖倒置转原则

使用场景
 ● 一个对象结构包含很多类对象,它们有不同的接口,而你想对这些对象实施一些依赖 于其具体类的操作,也就说是用迭代器模式已经不能胜任的情景。 

● 需要对一个对象结构中的对象进行很多不同并且不相关的操作,而你想避免让这些操 作“污染”这些对象的类。


扩展实例
 
1.统计功能
 统计工资total只需要在IVisitor抽象中添加一个public int getTotalSalary();然后在具体访问者中实现即可。 

public class Visitor implements IVisitor {//部门经理的工资系数是5private final static int MANAGER_COEFFICIENT = 5;//员工的工资系数是2private 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;}}

2.多个访问者
由展示->汇总
IShowVisitor extends IVisitor 
ITotalVisitor  extends IVisitor

双分派
java的静态绑定和动态绑定,多态,重写,重载。 

你调用我的,我调用你的。谁是主谁是次,真正在哪里处理问题。 


我是菜鸟,我在路上。

0 0