设计模式——访问者模式
来源:互联网 发布:期货1分钟数据 编辑:程序博客网 时间:2024/06/06 01:13
访问者模式:对于一组对象,在不改变数据结构的前提下,增加作用于这些结构元素新的功能。
适用于数据结构相对稳定,它把数据结构和作用于其上的操作解耦,使得操作集合可以相对自由地演化。
优点:
符合单一职责原则
扩展性良好
有益于系统的管理和维护
缺点:
增加新的元素类变得很困难
破坏封装性
注意事项:
系统有比较稳定的数据结构
与迭代器的关系
适用场合:
如果一个系统有比较稳定的数据结构,又有经常变化的功能需求,那么访问者模式就是比较合适的
Visitor .class
public interface Visitor { abstract public void Visit( Element element );}
CompensationVisitor.class
public class CompensationVisitor implements Visitor { @Override public void Visit(Element element) { // TODO Auto-generated method stub Employee employee = ((Employee) element); System.out.println(employee.getName() + "'s Compensation is " + (employee.getDegree() * employee.getVacationDays() * 10)); }}
Employee .class
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 void setName(String name) { this.name = name; } public String getName() { return name; } public void setIncome(float income) { this.income = income; } public float getIncome() { return income; } public void setVacationDays(int vacationDays) { this.vacationDays = vacationDays; } public int getVacationDays() { return vacationDays; } public void setDegree(int degree) { this.degree = degree; } public int getDegree() { return degree; } @Override public void Accept(Visitor visitor) { // TODO Auto-generated method stub visitor.Visit(this); }}
Element .class
public abstract class Element { abstract public void Accept(Visitor visitor);}
Employees .class
mport java.util.ArrayList;import java.util.HashMap;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); } }}
MainTest .class
import java.util.Random;public class MainTest { public static void main(String[] args) { Employees mEmployees = new Employees(); mEmployees.Attach(new Employee("Tom", 4500, 8, 1)); mEmployees.Attach(new Employee("Jerry", 6500, 10, 2)); mEmployees.Attach(new Employee("Jack", 9600, 12, 3)); mEmployees.Accept(new CompensationVisitor()); }}
0 0
- 设计模式——访问者
- 设计模式——访问者
- 设计模式——访问者模式(Visitor)
- java设计模式——访问者模式
- 设计模式(21)——访问者模式
- PHP设计模式——访问者模式
- 设计模式——访问者模式
- 设计模式GOF23——访问者模式
- C++设计模式——访问者模式
- 设计模式——访问者模式
- 设计模式——访问者模式
- 设计模式—Visitor访问者模式
- 设计模式 — 访问者模式(Visitor Method)
- JAVA设计模式—访问者模式(Visitor)
- 大话设计模式—访问者模式
- C#设计模式—访问者模式
- Java设计模式—访问者模式
- 设计模式 访问者模式
- android4.4.2 短信广播变更
- JSON.parse()和JSON.stringify()
- dwz 框架参数传递
- 一些常见的AAC编解码库
- MySQL删除表中多余的重复记录,只保留一条记录
- 设计模式——访问者模式
- 怎么样使用yum来安装mysql
- 【ife】任务十一:移动Web页面布局实践
- SSL JudgeOnline 1194——最佳乘车
- Intermediate Core Graphics(Swift)三
- 状态模式
- 用Python进行梯度提升算法的参数调整
- mac 如何禁止 Google Chrome 自动更新
- Linux下启动Java程序