概述
访问者模式是一个个人认为比较绕的设计模式,它有两个比较常用的用途
第一:给某个已有的对象在运行时时期添加某个方法,这个方法就是在Visitor类中实现的方法。
声明我需要添加哪个方法,以及实现这个方法需要谁的帮助。这里Leader 要给老板作报告,但是他不会抓了个雇员从参数里面扔进来。
public interface Visitor { public void doAsTheBossOrder(Employee me); }
|
public class Leader implements Visitor{ public void doAsTheBossOrder(Employee me) { me.operation1(); me.operation2(); me.operation3(); reportToBoss(); } public void reportToBoss() { System.out.println("这是我办的"); } }
|
public abstract class Employee { public abstract void accept(Visitor leader); public void operation1() { System.out.println("施展我的技能1"); } public void operation2() { System.out.println("施展我的技能2"); } public void operation3() { System.out.println("施展我的技能3"); } }
|
public class Engeener extends Employee{ public void accept(Visitor leader) { leader.doAsTheBossOrder(this); } }
|
public class Client { public static void main(String args[]) { Employee engeener = new Engeener(); Visitor leader = new Leader(); engeener.accept(leader); } }
|
我们从上面例子可以看出Engeener类并没有doAsTheBossOrder的方法,但是调用engeener.accept(leader)的时候实际上运行的就是doAsTheBossOrder方法,这相当于在运行时的时候Engeener临时添加了一个doAsTheBossOrder方法。这个和装饰模式有存在很大的差异,虽然二者都是通过组合对象来实现的,但是装饰模式是一种静态添加的,添加后装饰类就拥有了这个方法,而访问者模式中是动态添加的,并没有在类代码中实际添加该方法。
遍历不用结构的元素:
public interface Visitor { public void visit(CommentEmployee employee); public void visit(Leader leader); }
|
public class ConcreVisitor implements Visitor {
public void visit(CommentEmployee employee) { System.out.println(this.getCommonEmployeeInfo(employee)); }
public void visit(Leader leader) { System.out.println(this.getLeaderInfo(leader)); }
private String getBasicInfo(Employee employee) { return "Id :" +employee.getId() + "Name :"+employee.getName() +"Sex :"+((employee.getSex() == 0)? "MAN":"LAYDY") +"Salary :" + employee.getSalary(); }
private String getLeaderInfo(Leader leader) { String basicInfo = this.getBasicInfo(leader); String otherInfo = "Performance :"+leader.getPerformence(); return basicInfo + otherInfo; }
private String getCommonEmployeeInfo(CommentEmployee employee) { String basicInfo = this.getBasicInfo(employee); String otherInfo = "Job :"+employee.getJob(); return basicInfo + otherInfo; } }
|
public abstract class Employee {
private long id; private String name; private int sex; private int salary;
public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public abstract void accept(Visitor visitor); }
|
public class CommentEmployee extends Employee { private String job; public String getJob() { return job; } public void setJob(String job) { this.job = job; } public void accept(Visitor visitor) { visitor.visit(this); } }
|
public class Leader extends Employee {
private String performence; public String getPerformence() { return performence; } public void setPerformence(String performence) { this.performence = performence; } public void accept(Visitor visitor) { visitor.visit(this); } }
|
import java.awt.List; import java.util.ArrayList;
public class ObjectStruct {
private ArrayList<Employee> memberlist = new ArrayList<Employee>();
public void addMember(Employee employee) { memberlist.add(employee); }
public void handleRequest() { for(Employee em: memberlist) { Visitor visitor = new ConcreVisitor(); em.accept(visitor); } } }
|
public class Client {
public static void main(String args[]) { ObjectStruct objectStruce = new ObjectStruct(); CommentEmployee em1 = new CommentEmployee(); em1.setId(1); em1.setName("jimmy1"); em1.setSex(0); em1.setSalary(10000); em1.setJob("码农"); objectStruce.addMember(em1);
Leader em2 = new Leader(); em2.setId(2); em2.setName("hello ketty"); em2.setSex(3); em2.setSalary(40000); em2.setPerformence("Well done"); objectStruce.addMember(em2);
objectStruce.handleRequest(); } }
|