二十三种设计模式之访问者模式

来源:互联网 发布:java 工具类打包jar包 编辑:程序博客网 时间:2024/06/06 19:04

访问者模式原理:对于一组对象,在不改变数据结构的前提下,增加作用与这些结构元素的新功能.

应用场景:雇员系统 年假折合薪资的功能

传统实现方式


public class Employee {    private String name;    private float income;    private int vacationDays;    private int degree;    public Employee(String name, float income, int vacationDays, int degree) {        this.name = name;        this.income = income;        this.vacationDays = vacationDays;        this.degree = degree;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public float getIncome() {        return income;    }    public void setIncome(float income) {        this.income = income;    }    public int getVacationDays() {        return vacationDays;    }    public void setVacationDays(int vacationDays) {        this.vacationDays = vacationDays;    }    public int getDegree() {        return degree;    }    public void setDegree(int degree) {        this.degree = degree;    }}

public class Employees {    private HashMap<String,Employee> employees;    public Employees() {        employees = new HashMap<String,Employee>();    }    public void Attach(Employee employee){        employees.put(employee.getName(),employee);    }    public void Detach(Employee employee){        employees.remove(employee);    }    public Employee getEmployee(String name){        return employees.get(name);    }    public void getCompenstation(){        for(Employee employee:employees.values()){            System.out.println(employee.getName()+"'s Compensation is "                    +employee.getDegree()*employee.getVacationDays()*100);        }    }}

public class MainTest {    public static void main(String[] args) {        Employees employees = new Employees();        employees.Attach(new Employee("Tom",4500,8,1));        employees.Attach(new Employee("Jerry",6500,10,2));        employees.Attach(new Employee("Jack",9600,12,3));        employees.getCompenstation();    }}


visitor模式实现

public abstract class Element {    abstract public void Accept(Visitor visitor);}

public interface Visitor {    abstract public void Visit(Element element);}
public class CompensationVisitor implements Visitor {    @Override    public void Visit(Element element) {        Employee employee = (Employee) element;        System.out.println(employee.getName()+"'s Compensation is "+(employee.getDegree()*employee.getVacationDays()*10));    }}
public class Employee extends Element {    private String name;    private float income;    private int vacationDays;    private int degree;    public Employee(String name, float income, int vacationDays, int degree) {        this.name = name;        this.income = income;        this.vacationDays = vacationDays;        this.degree = degree;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public float getIncome() {        return income;    }    public void setIncome(float income) {        this.income = income;    }    public int getVacationDays() {        return vacationDays;    }    public void setVacationDays(int vacationDays) {        this.vacationDays = vacationDays;    }    public int getDegree() {        return degree;    }    public void setDegree(int degree) {        this.degree = degree;    }    @Override    public void Accept(Visitor visitor) {        visitor.Visit(this);    }}

public class Employees {    private HashMap<String, Employee> employees;    public Employees() {        employees = new HashMap();    }    public void Attach(Employee employee) {        employees.put(employee.getName(), employee);    }    public void Detach(Employee employee) {        employees.remove(employee);    }    public Employee getEmployee(String name) {        return employees.get(name);    }    public void Accept(Visitor visitor) {        for (Employee e : employees.values()) {            e.Accept(visitor);        }    }}
public class MainTest {    public static void main(String[] args) {        Employees employees = new Employees();        employees.Attach(new Employee("Tom",4500,8,1));        employees.Attach(new Employee("Jerry",6500,10,2));        employees.Attach(new Employee("Jack",9600,12,3));        employees.Accept(new CompensationVisitor());    }}

原创粉丝点击