Notice that there is no indication what the
Notice that there is no indication what the Visitor does with teach class in either the client classes or the abstract Visitor class. We can in fact write a whole lot of visitors that do different things to the classes in our program. The Visitor we are going to write first just sums the vacation data for all our employees: public class VacationVisitor extends Visitor { protected int total_days; public VacationVisitor() { total_days = 0; } //—————————-public void visit(Employee emp) { total_days += emp.getVacDays(); } //—————————-public int getTotalDays() { return total_days; } } Visiting the Classes Now, all we have to do to compute the total vacation taken is to go through a list of the employees and visit each of them, and then ask the Visitor for the total. VacationVisitor vac = new VacationVisitor(); for (int i = 0; i < employees.length; i++) { employees[i].accept(vac); } System.out.println(vac.getTotalDays()); Let s reiterate what happens for each visit: 1. We move through a loop of all the Employees. 2. The Visitor calls each Employee s accept method. 3. That instance of Employee calls the Visitor s visit method. 4. The Visitor fetches the vacation days and adds them into the total. 5. The main program prints out the total when the loop is complete.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services