/**
* 员工接受不同部门的数据访问。
* 但是不同部门访问该名员工获取的数据不相同。
*
* @author zhangfly
*
* @param <T>
*/
public abstract class Employee<T> {
public abstract void accept(Department department);
public String getName() {
return null;
}
public int getYear() {
return 0;
}
public T getInfo() {
return null;
}
}
/**
* 不同部门访问同一员工,访问的目的不同,获取的员工数据不同。
*
* @author zhangfly
*
*/
public abstract class Department {
public abstract void visit(Employee employee);
}
public class EmployeeImpl extends Employee {
private String name = "zhang";
private int year = 2019;
@Override
public void accept(Department department) {
department.visit(this);
}
@Override
public String getName() {
return name;
}
@Override
public int getYear() {
return year;
}
}
/**
* A,不同部门之一。
*
* @author zhangfly
*
*/
public class DepartmentA extends Department {
@Override
public void visit(Employee employee) {
System.out.println("Department A 访问-> " + employee.getName());
}
}
/**
* B,不同部门之一。
*
* @author zhangfly
*
*/
public class DepartmentB extends Department {
@Override
public void visit(Employee employee) {
System.out.println("Department B 访问-> " + employee.getYear());
}
}
public class Test {
public Test() {
Employee employee = new EmployeeImpl();
Department departmentA = new DepartmentA();
Department departmentB = new DepartmentB();
departmentA.visit(employee);
departmentB.visit(employee);
System.out.println("###分割线###");
employee.accept(departmentB);
employee.accept(departmentA);
}
public static void main(String[] args) {
new Test();
}
}
输出:
Department A 访问-> zhang
Department B 访问-> 2019
###分割线###
Department B 访问-> 2019
Department A 访问-> zhang