• 訪问者模式:
    • 一个作用于某对象结构中各元素的操作,使你能够在不改变各元素类数据结构的前提下添加作用于这些元素的新操作。

    • 结构对象是訪问者模式必备条件。且这个结构对象必须存在遍历自身各个对象的方法。

    • 适用于:数据结构相对稳定,把数据结构和作用与其上的其他操作解耦,使得操作相对自由。
    • 长处:
    • 1、符合单一职责原则
    • 2、扩展性良好:元素类能够通过接受不同的訪问者来实现对不同操作的扩展。

    • 缺点:
    • 1、假设要添加新元素。则会让操作变得更复杂
    • 2、在一定程序上破坏了封装性原则
      *
      訪问者械五大角色对象:
      1.Visitor 抽象訪问者角色
      2.ConcreteVisitor.详细訪问者角色
      3.Element 接受訪问操作元素
      4.ConcreteElement 详细元素
      5.ObjectStructure 结构对象角色,这是使用訪问者模式必备的角色。

/**
 * 抽象訪问者:为该对象结构中详细元素角色声明一个訪问操作接口。

* 该操作接口的名字和參数标识了发送訪问请求给详细訪问者的详细元素角色, * 这样訪问者就能够通过该元素角色的特定接口直接訪问它。 * @description: * @date 2016-1-15 下午4:00:29 */ public interface Visitor { void visit(Element element); }

/**
 * 详细訪问者角色,实现Visitor声明的接口。
 * @description:
 * @date 2016-1-15 下午4:20:46
 */
public class ConcreteVisitor implements Visitor{

    @Override
    public void visit(Element element) {
        Staff e=(Staff) element;
        //比方:加薪的计算方式:职位*加薪系数+工龄*相应系统之和  除以10 再乘以如今工资
        System.out.println(e.getName()+"要加的薪水是:"+(e.getDegree()*0.8+e.getWorkAges()*0.5)/10*e.getSalary());//
    }
}
/**
 * 定义一个接受訪问操作类,訪问者(Visitor)操作函数的參数。
 * @description:
 * @date 2016-1-15 下午3:58:28
 */
public abstract class Element {
    public abstract void Acceppt(Visitor visitor);
}
/**
 * 详细元素,实现了抽象元素(Element)所定义的接受操作接口。
 * @description:
 * @date 2016-1-15 下午4:04:24
 */
public class Staff extends Element {
    private String name;
    private float salary;
    private int workAges;
    private int degree;

    public Staff(String name, float salary, int workAges, int degree) {
        super();
        this.name = name;
        this.salary = salary;
        this.workAges = workAges;
        this.degree = degree;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

    public int getWorkAges() {
        return workAges;
    }

    public void setWorkAges(int workAges) {
        this.workAges = workAges;
    }

    public int getDegree() {
        return degree;
    }

    public void setDegree(int degree) {
        this.degree = degree;
    }

    @Override
    public void Acceppt(Visitor visitor) {
        visitor.visit(this);
    }

}
/**
 * 结构对象:这是使用訪问者模式必备的角色。

* 它具备下面特性: * 能枚举它的元素; * 能够提供一个高层接口以同意訪问者訪问它的元素; * 如有须要,能够设计成一个复合对象或者一个聚集(如一个列表或无序集合)。 * @description: * @date 2016-1-15 下午4:26:30 */ public class StaffObject { private HashMap<String, Staff> employees; public StaffObject() { employees = new HashMap<String, Staff>(); } public void addEmployee(Staff e) { if (!employees.containsKey(e.getName())) { employees.put(e.getName(), e); } } public void removeEmployee(Staff e) { if (employees.containsKey(e.getName())) { employees.remove(e); } } public Staff getEmployee(String name) { return employees.get(name); } public void Accept(Visitor v) { for (Staff e : employees.values()) { e.Acceppt(v); } } }

測试类

public class Test {
    public static void main(String[] args) {
        StaffObject e=new StaffObject();
        e.addEmployee(new Staff("张三", 3000f, 2, 1));
        e.addEmployee(new Staff("李四", 5000f, 4, 2));
        e.addEmployee(new Staff("王五", 8000f, 6, 3));
        e.addEmployee(new Staff("沈七", 10000f, 9, 4));
        e.Accept(new ConcreteVisitor());

    }

}

执行结果:
李四要加的薪水是:1800.0
张三要加的薪水是:540.0
沈七要加的薪水是:7700.0
王五要加的薪水是:4320.0