标签:new rgs static employee 不同的 time() string bsp 针对
当想为一个对象的组合增加新的能力,并且封装并不重要时。
一个对象结构包含多个对象,希望对这些对象实施一些依赖其具体类型的操作,在访问者中针对每一个具体的类型都提供一个访问操作,不同类型的对象可以有不同的访问操作;或者需要对一个对象结构中的对象进行很多不同的且不相关的操作时,要避免让这些操作“污染”这些对象的类,也不希望在增加新操作时修改这些类,该模式使得可将相关访问的操作几种起来定义在访问者类中,对象结构可被多个不同的访问者类所使用,将对象本身与对象访问操作分离。
适用场景:OA中员工数据汇总,奖励审批系统
1 //部门类的抽象访问接口 2 public interface Department { 3 4 public void visit(ZSEmployee employee);//访问正式员工 5 public void visit(LSEmployee employee);//访问临时员工 6 7 } 8 9 //员工类的抽象访问接口 10 public interface Employee { 11 12 public void accept(Department visitor);//接受一个访问者访问 13 14 } 15 16 //正式员工类 17 public class ZSEmployee implements Employee { 18 19 private String name; 20 private int monthWorkTime; 21 22 public ZSEmployee(String name, int monthWorkTime){ 23 this.name = name; 24 this.monthWorkTime = monthWorkTime; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 public int getMonthWorkTime() { 36 return monthWorkTime; 37 } 38 39 public void setMonthWorkTime(int monthWorkTime) { 40 this.monthWorkTime = monthWorkTime; 41 } 42 43 @Override 44 public void accept(Department visitor){ 45 visitor.visit(this); 46 } 47 48 } 49 50 //临时员工类 51 public class LSEmployee implements Employee { 52 53 private String name; 54 private int dayWorkTime; 55 56 public LSEmployee(String name, int dayWorkTime){ 57 this.name = name; 58 this.dayWorkTime = dayWorkTime; 59 } 60 61 public String getName() { 62 return name; 63 } 64 65 public void setName(String name) { 66 this.name = name; 67 } 68 69 public int getDayWorkTime() { 70 return dayWorkTime; 71 } 72 73 public void setDayWorkTime(int dayWorkTime) { 74 this.dayWorkTime = dayWorkTime; 75 } 76 77 @Override 78 public void accept(Department visitor){ 79 visitor.visit(this); 80 } 81 82 } 83 84 //财务部类 85 public class FADepartment implements Department { 86 87 @Override 88 public void visit(ZSEmployee employee){ 89 employee.getMonthWorkTime();//访问正式员工的月工作时间 90 //根据月工作时间发工资 91 } 92 93 @Override 94 public void visit(LSEmployee employee){ 95 employee.getDayWorkTime();//访问临时员工的日工作时间 96 //根据日工作时间发工资 97 } 98 99 } 100 101 //HR部类 102 public class HRDepartment implements Department { 103 104 @Override 105 public void visit(ZSEmployee employee){ 106 employee.getMonthWorkTime();//访问正式员工的月工作时间 107 //根据月工作时间是否开除你 108 } 109 110 @Override 111 public void visit(LSEmployee employee){ 112 employee.getDayWorkTime();//访问临时员工的日工作时间 113 //根据日工作时间是否开除你 114 } 115 116 }
1 import java.util.LinkedList; 2 import java.util.List; 3 4 public class OAManager { 5 6 public static void main(String[] args) { 7 8 List<Employee> list = new LinkedList<>(); 9 list.add(new ZSEmployee("正式员工A", 300)); 10 list.add(new ZSEmployee("正式员工B", 400)); 11 list.add(new ZSEmployee("正式员工C", 200)); 12 list.add(new LSEmployee("临时员工A", 4)); 13 list.add(new LSEmployee("临时员工B", 8)); 14 15 Department HR = new HRDepartment(); 16 for(Employee employee: list){ 17 employee.accept(HR); 18 } 19 20 } 21 22 }
标签:new rgs static employee 不同的 time() string bsp 针对
原文地址:http://www.cnblogs.com/gsbm/p/7091581.html