image.png访问者模式(Visitor Pattern):提供一个作用于某对象结构中的各元素的操作表示,它使我们可以
在不改变各元素的类的前提下定义作用于这些元素的新操作。访问者模式是一种对象行为型
模式。
在访问者模式结构图中包含如下几个角色:
●Vistor(抽象访问者):抽象访问者为对象结构中每一个具体元素类ConcreteElement声明一
个访问操作,从这个操作的名称或参数类型可以清楚知道需要访问的具体元素的类型,具体
访问者需要实现这些操作方法,定义对这些元素的访问操作。
●ConcreteVisitor(具体访问者):具体访问者实现了每个由抽象访问者声明的操作,每一个操
作用于访问对象结构中一种类型的元素。
●Element(抽象元素):抽象元素一般是抽象类或者接口,它定义一个accept()方法,该方法
通常以一个抽象访问者作为参数。【稍后将介绍为什么要这样设计。】
●ConcreteElement(具体元素):具体元素实现了accept()方法,在accept()方法中调用访问者
的访问方法以便完成对一个元素的操作。
● ObjectStructure(对象结构):对象结构是一个元素的集合,它用于存放元素对象,并且提
供了遍历其内部元素的方法。它可以结合组合模式来实现,也可以是一个简单的集合对象,
如一个List对象或一个Set对象。
员工数据汇总
image.png//部门类:抽象访问者类
abstract class Department {
//声明一组重载的访问方法,用于访问不同类型的具体元素
public abstract void visit(FulltimeEmployee employee);
public abstract void visit(ParttimeEmployee employee);
}
-----------------------------------------------------------------------------------
//财务部类:具体访问者类
class FADepartment extends Department {
//实现财务部对全职员工的访问
public void visit(FulltimeEmployee employee) {
int workTime = employee.getWorkTime();
double weekWage = employee.getWeeklyWage();
if (workTime > 40) {
weekWage = weekWage + (workTime - 40) * 100;
} else if (workTime < 40) {
weekWage = weekWage - (40 - workTime) * 80;
if (weekWage < 0) {
weekWage = 0;
}
}
System.out.println("正式员工" + employee.getName() + "实际工资为:" + weekWage + "元。");
}
//实现财务部对兼职员工的访问
public void visit(ParttimeEmployee employee) {
int workTime = employee.getWorkTime();
double hourWage = employee.getHourWage();
System.out.println("临时工" + employee.getName() + "实际工资为:" + workTime * hourWage + "元。");
}
}
-----------------------------------------------------------------------------------
//人力资源部类:具体访问者类
class HRDepartment extends Department {
//实现人力资源部对全职员工的访问
public void visit(FulltimeEmployee employee) {
int workTime = employee.getWorkTime();
System.out.println("正式员工" + employee.getName() + "实际工作时间为:" + workTime + "小时。");
if (workTime > 40) {
System.out.println("正式员工" + employee.getName() + "加班时间为:" + (workTime - 40) + "小时。");
} else if (workTime < 40) {
System.out.println("正式员工" + employee.getName() + "请假时间为:" + (40 - workTime) + "小时。");
}
}
//实现人力资源部对兼职员工的访问
public void visit(ParttimeEmployee employee) {
int workTime = employee.getWorkTime();
System.out.println("临时工" + employee.getName() + "实际工作时间为:" + workTime + "小时。");
}
}
//员工类:抽象元素类
interface Employee {
public void accept(Department handler); //接受一个抽象访问者访问
}
-----------------------------------------------------------------------------------
//全职员工类:具体元素类
class FulltimeEmployee implements Employee {
private String name;
private double weeklyWage;
private int workTime;
public FulltimeEmployee(String name, double weeklyWage, int workTime) {
this.name = name;
this.weeklyWage = weeklyWage;
this.workTime = workTime;
}
public void setName(String name) {
this.name = name;
}
public void setWeeklyWage(double weeklyWage) {
this.weeklyWage = weeklyWage;
}
public void setWorkTime(int workTime) {
this.workTime = workTime;
}
public String getName() {
return (this.name);
}
public double getWeeklyWage() {
return (this.weeklyWage);
}
public int getWorkTime() {
return (this.workTime);
}
public void accept(Department handler) {
handler.visit(this); //调用访问者的访问方法
}
}
-----------------------------------------------------------------------------------
class ParttimeEmployee implements Employee {
private String name;
private double hourWage;
private int workTime;
public ParttimeEmployee(String name, double hourWage, int workTime) {
this.name = name;
this.hourWage = hourWage;
this.workTime = workTime;
}
public void setName(String name) {
this.name = name;
}
public void setHourWage(double hourWage) {
this.hourWage = hourWage;
}
public void setWorkTime(int workTime) {
this.workTime = workTime;
}
public String getName() {
return (this.name);
}
public double getHourWage() {
return (this.hourWage);
}
public int getWorkTime() {
return (this.workTime);
}
public void accept(Department handler) {
handler.visit(this); //调用访问者的访问方法
}
}
//员工列表类:对象结构
class EmployeeList {
//定义一个集合用于存储员工对象
private ArrayList<Employee> list = new ArrayList<Employee>();
public void addEmployee(Employee employee) {
list.add(employee);
}
//遍历访问员工集合中的每一个员工对象
public void accept(Department handler) {
for (Object obj : list) {
((Employee) obj).accept(handler);
}
}
}
class Client {
public static void main(String args[]) {
EmployeeList list = new EmployeeList();
Employee fte1, fte2, fte3, pte1, pte2;
fte1 = new FulltimeEmployee("张无忌", 3200.00, 45);
fte2 = new FulltimeEmployee("杨过", 2000.00, 40);
fte3 = new FulltimeEmployee("段誉", 2400.00, 38);
pte1 = new ParttimeEmployee("洪七公", 80.00, 20);
pte2 = new ParttimeEmployee("郭靖", 60.00, 18);
list.addEmployee(fte1);
list.addEmployee(fte2);
list.addEmployee(fte3);
list.addEmployee(pte1);
list.addEmployee(pte2);
Department dep;
dep = new FADepartment();
list.accept(dep);
}
}
image.png
网友评论