Java Reference
In-Depth Information
Example 2.51 ProjectCostVisitor.java
1. public class ProjectCostVisitor implements ProjectVisitor{
2. private double totalCost;
3. private double hourlyRate;
4.
5. public double getHourlyRate(){ return hourlyRate; }
6. public double getTotalCost(){ return totalCost; }
7.
8. public void setHourlyRate(double rate){ hourlyRate = rate; }
9.
10. public void resetTotalCost(){ totalCost = 0.0; }
11.
12. public void visitDependentTask(DependentTask p){
13. double taskCost = p.getTimeRequired() * hourlyRate;
14. taskCost *= p.getDependencyWeightingFactor();
15. totalCost += taskCost;
16. }
17. public void visitDeliverable(Deliverable p){
18. totalCost += p.getMaterialsCost() + p.getProductionCost();
19. }
20. public void visitTask(Task p){
21. totalCost += p.getTimeRequired() * hourlyRate;
22. }
23. public void visitProject(Project p){ }
24. }
All behavior for the calculation, as well as variable storage, is centralized in the Visitor class. To add a new
behavior, you would create a new class that implements ProjectVisitor and redefine the four visit methods.
Search WWH ::




Custom Search