I. Overview
The visitor pattern is a more complex, behavioral design pattern that contains the two main components of the visitor and the accessed element, which are often of different types, and that different visitors can perform different access operations on them. When using the visitor pattern, the elements that are accessed are usually not separate, they are stored in a collection, which is called the "object Structure", and the visitor iterates through the object structure to manipulate the elements stored therein. The visitor pattern is an object-behavioral pattern.
Second, the application scenario
When there are multiple types of visitors (or operators) manipulating a set of object collections (or object structures), where the collection of objects also contains multiple types of objects, different visitor types provide different access actions for each specific visitor object. Each visitor type object has different access actions for different audiences, so this scenario is very suitable for the visitor pattern.
If the preceding sentences are not clear, then the small LU is an example of a business scenario in life:
Your company every month the Human resources department to all employees to work hours, overtime hours statistics, and the finance department to all employees payroll accounting, the different positions of the staff salary accounting standards certainly not the same ah, this everyone understand. In this case, the Human resources department and the finance department are two different types of departments (visitors), all employees (who are visitors) are a collection of objects, and employees are divided into two types of managers and technicians (note: Here, the small LV is simply divided into two categories), in the monthly statistics, The HR department needs to make statistics on the hours of work and overtime of the employees, and the finance Department needs to make payroll accounting for the employees of different positions, which can be seen in different departments, and different in the operation of the employees, and the access operation of each department to different types of employees. Then it is necessary to understand the visitor pattern for this scenario.
Third, UML class diagram
Iv. participants
1>, Visitor (abstract visitor): declares an access operation for each specific visitor (concreteelement);
2>, Concretevisitor (Specific visitor): to achieve the specific access to the visitor (Concreteelement) operation;
3>, Element (abstract by visitor): there is usually an accept method to receive/reference an abstract visitor object;
4>, Concreteelement (target audience): Implement the Accept abstract method, through the specific visitor parameters passed in, call the specific visitor to the object's access operation method to implement the access logic;
5>, Clent, objectstructure (Client access Process test environment): In this process, the visitor is usually a collection object, through the traversal of the collection to complete the visitors to each of the accessed elements of the operation;
V. Use case Learning
1. Abstract by visitor: Company employees abstract class Employee.java
/** * Company employee (visitor) Abstract class * @author [email protected] * */public abstract class Employee {/** * receive/reference an abstract visitor object * @param Depa Rtment Abstract visitor Here refers to the company department such as Human Resources department, Finance Department */public Abstract Void Accept (Department Department);}
2. Specific visitor: Company management position employee categoryManageremployee.java
/** * Company employee: Manager (Specific Visitor object) * @author [email protected] * */public class Manageremployee extends Employee {//Employee name PRI Vate String name;//daily to work long private int timesheet; Monthly salary private double wage;//leave/late penalty length private int punishmenttime;public manageremployee (String name, int timesheet, Doub Le wage, int punishmenttime) {this.name = Name;this.timesheet = Timesheet;this.wage = Wage;this.punishmenttime = Punishmen Ttime;} @Overridepublic void Accept (Department Department) {department.visit (this);} /** * Get the actual hours per month = Work hours per day * Number of days to work per month-duration of penalty * @return */public int gettotaltimesheet () {return timesheet * 22-punishmentti Me;} /** * Get monthly actual salary payable = monthly fixed wage-duration of penalty * 5<br/> * < as company manager every 1 hours late deduction 5 dollars > * @return */public double gettotalwage () {RET Urn Wage-punishmenttime * 5;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public double Getwage () {return wage;} public void Setwage (double wage) {this.wage = wage;} public int Getpunishmenttime () {return PuniShmenttime;} public void setpunishmenttime (int punishmenttime) {this.punishmenttime = Punishmenttime;}}
3. Specific visitor: Company General post employee categoryGeneralemployee.java
/** * Company General Staff (specific audience) * @author [email protected] * */public class Generalemployee extends Employee {//Employee name PR Ivate String name;//hours per day private int timesheet;//monthly salary private double wage;//leave/late penalty length private int punishmenttime;public Generalemployee (String name, int timesheet, double wage, int punishmenttime) {this.name = Name;this.timesheet = Timesheet ; this.wage = Wage;this.punishmenttime = Punishmenttime;} @Overridepublic void Accept (Department Department) {department.visit (this);} /** * Get the actual hours per month = Work hours per day * Number of days to work per month-duration of penalty * @return */public int gettotaltimesheet () {return timesheet * 22-punishmentt IME;} /** * Get the actual monthly salary payable = monthly fixed wage-duration of penalty * 10<br/> * < as the company's ordinary staff every 1 hours late buckle 10 dollars pit it? haha > * * @return */public double gettotalwage () {return wage-punishmenttime * 10;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public double Getwage () {return wage;} public void Setwage (double wage) {this.wage = wage;} public int GetpunishmentTime () {return punishmenttime;} public void setpunishmenttime (int punishmenttime) {this.punishmenttime = Punishmenttime;}}
4. Abstract Visitor: Company Department abstract classDepartment.java
/** * Company Department (visitor) abstract class * @author [email protected] * */public abstract class Department {//declares a set of overloaded access methods for accessing different types of concrete elements (referred to here is a different employee) /** * Abstract method to access Company manager Objects <br/> * Specific access to what the object is by the specific visitor subclass (here refers to different specific departments) to implement * @param me */public abstract Voi D Visit (manageremployee me);/** * Abstract method to access the company's ordinary employees object <br/> * What specifically accesses an object is implemented by a specific visitor subclass (this refers to a different specific department) @param GE */pu Blic abstract void Visit (Generalemployee ge);}
5. Specific visitor: Corporate Finance ClassFadepartment.java
/** * Specific visitors: Corporate Finance <br/> * The responsibility of the Finance Department is to be responsible for statistical accounting of employees ' salaries * @author [email protected] * */public class Fadepartment extends Department {/** * monthly salary for access to Company manager objects */@Overridepublic void visit (Manageremployee me) {Double totalwage = Me.gettotalwage (); S Ystem.out.println ("Manager:" + me.getname () + " fixed wage =" + me.getwage () + ", Late duration" + me.getpunishmenttime () + "hour" + ", real [+totalwage];} /** * Monthly salary of access to the company's ordinary employee objects */@Overridepublic void visit (Generalemployee ge) {Double totalwage = Ge.gettotalwage (); SYSTEM.OUT.PRINTLN ("Normal employee:" + ge.getname () + " fixed wage =" + ge.getwage () + ", late Time" + ge.getpunishmenttime () + "hour" + ", real Payroll = "+totalwage);}}
6. Specific visitor: Company Human Resources classHrdepartment.java
/** * Specific Visitor object: Human Resources Department <br/> * HR Department is responsible for statistical accounting staff's monthly working hours * @author [email protected] * */public class Hrdepartmen T extends Department {/** * accesses the monthly actual hours of work of the Company manager Object */@Overridepublic void visit (manageremployee me) {Me.gettotaltimesheet ( );} /** * Monthly actual hours of work of the company's average employee are counted */@Overridepublic void visit (generalemployee ge) {ge.gettotaltimesheet ();}}
7. Client-side test class: Simulate the payroll accounting and access to the company's employees by the finance department Client.java
Import Java.util.arraylist;import Java.util.list;public class Client {public static void main (string[] args) {list< employee> employeelist = new arraylist<employee> (); Employee mep1,mep2,gep1,gep2,gep3;//Manager 1mep1 = new Manageremployee ("Wang Total", 8, 20000, 10);//Manager 2MEP2 = new Manageremployee ( "Manager Xie", 8, 15000, 15);//General Staff 1GEP1 = new Generalemployee ("Xiao Jie", 8, 8000, 8);//General Staff 2GEP2 = new Generalemployee ("Xiao Xiao", 8, 8500 , 12);//General Staff 3GEP3 = new Generalemployee ("Xiao Hu", 8, 7500, 0); Employeelist.add (MEP1); Employeelist.add (MEP2); Employeelist.add (GEP1); Employeelist.add (GEP2); Employeelist.add (GEP3);//Finance Department Payroll Accounting/access to company employees Fadepartment Department = New Fadepartment (); for (Employee employee:employeelist) {employee.accept (department);}}}
If you want to change the one-month time-of-work statistic for HR to employees, simply
Fadepartment Department = new Fadepartment ();
Modify the following to
Hrdepartment Department = new Hrdepartment ();
8. Program Operation Result:
Manager: Wang total fixed wages = 20000.0, 10 hours late, real wages = 19950.0 managers: Manager Xie's fixed salary = 15000.0, is late for 15 hours, real wages = 14925.0 Ordinary Employees: Jay Fixed Salary =80 00.0, 8 hours late, real wages = 7920.0 Ordinary Employees: Xiao Xiao fixed wages = 8500.0, late for 12 hours, real wages = 8380.0 Ordinary staff: Small tiger fixed wages = 7500.0, late for 0 hours, the actual wage =7500 .0
Vi. Other
Java Design Pattern Visitor pattern "Visitor pattern"