Java Reference
In-Depth Information
14. public String getDescription(){ return description; }
15. public ArrayList getProjectItems(){ return projectItems; }
16.
17. public void setName(String newName){ name = newName; }
18. public void setDescription(String newDescription){ description = newDescription; }
19.
20. public void addProjectItem(ProjectItem element){
21. if (!projectItems.contains(element)){
22. projectItems.add(element);
23. }
24. }
25.
26. public void removeProjectItem(ProjectItem element){
27. projectItems.remove(element);
28. }
29.
30. public void accept(ProjectVisitor v){
31. v.visitProject(this);
32. }
33. }
Example 2.49 Task.java
1. import java.util.ArrayList;
2. public class Task implements ProjectItem{
3. private String name;
4. private ArrayList projectItems = new ArrayList();
5. private Contact owner;
6. private double timeRequired;
7.
8. public Task(){ }
9. public Task(String newName, Contact newOwner,
10. double newTimeRequired){
11. name = newName;
12. owner = newOwner;
13. timeRequired = newTimeRequired;
14. }
15.
16. public String getName(){ return name; }
17. public ArrayList getProjectItems(){ return projectItems; }
18. public Contact getOwner(){ return owner; }
19. public double getTimeRequired(){ return timeRequired; }
20.
21. public void setName(String newName){ name = newName; }
22. public void setOwner(Contact newOwner){ owner = newOwner; }
23. public void setTimeRequired(double newTimeRequired){ timeRequired = newTimeRequired; }
24.
25. public void addProjectItem(ProjectItem element){
26. if (!projectItems.contains(element)){
27. projectItems.add(element);
28. }
29. }
30.
31. public void removeProjectItem(ProjectItem element){
32. projectItems.remove(element);
33. }
34.
35. public void accept(ProjectVisitor v){
36. v.visitTask(this);
37. }
38. }
The basic interface that defines the Visitor behavior is the ProjectVisitor. It defines a visit method for
each of the project classes.
Example 2.50 ProjectVisitor.java
1. public interface ProjectVisitor{
2. public void visitDependentTask(DependentTask p);
3. public void visitDeliverable(Deliverable p);
4. public void visitTask(Task p);
5. public void visitProject(Project p);
6. }
With this framework in place, you can define classes that implement the ProjectVisitor interface and perform
some computation on project items. The class ProjectCostVisitor shows how project cost calculations could
be managed.
Search WWH ::




Custom Search