二十三种设计模式之访问者模式
来源:互联网 发布: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()); }}
阅读全文
0 0
- 二十三种设计模式之访问者模式
- C++设计模式之二十三:Visitor(访问者模式)
- 行为型:设计模式之访问者模式(二十三)
- (二十三)Java设计模式之访问者模式
- 二十三种设计模式之行为型模式之访问者模式
- 二十三、访问者模式
- 设计模式(二十三)----- 访问者模式(visitor)----(JAVA版)
- 设计模式C++实现二十三:访问者模式
- 小话设计模式(二十三)访问者模式
- PHP设计模式系列(二十三):访问者模式
- 设计模式—访问者模式(二十三)
- java设计模式之访问者模式二
- java 开发模式之二十三:访问者模式
- Java设计模式十三:访问者模式(Visitor)
- 设计模式之二十------Visitor(访问者)
- 二十三 设计模式之桥梁模式
- 23种设计模式之访问者模式
- 23种设计模式之访问者模式
- 自定义注解
- 设计模式学习笔记(抽象工厂模式)
- [leetcode]--26. Remove Duplicates from Sorted Array
- Spring中IoC的入门实例
- 最小K数之和
- 二十三种设计模式之访问者模式
- 3.1.3 WebView 文件下载、缓存、内存泄露
- sqlmap做sql注入测试
- 【ZZULIOJ】 2181 GJJ的日常之暴富梦
- NYOJ20
- java文件上传
- 【HDU1166敌兵布阵](树状数组)
- HandlerMapping和BeanNameUrlHandlerMapping的使用
- Codeforces812C Sagheer and Nubian Market(二分查找)