Java Reference
In-Depth Information
The Task class represents a job that can consist of any number of subtasks or deliverables. For this reason,
getTimeRequired calculates the total time for the Task and all its children by iterating through its list of project
items and calling the getTimeRequired method. The method getMaterialsCost follows a similar strategy,
working through the list of project items and calling each child's getMaterialsCost method.
Example A.131 Task.java
1. import java.util.ArrayList;
2. import java.util.Iterator;
3. public class Task extends ProjectItem{
4. private ArrayList projectItems = new ArrayList();
5. private double taskTimeRequired;
6.
7. public Task(){ }
8. public Task(String newName, String newDescription,
9. double newTaskTimeRequired, double newRate){
10. super(newName, newDescription, newRate);
11. taskTimeRequired = newTaskTimeRequired;
12. }
13.
14. public void setTaskTimeRequired(double newTaskTimeRequired){ taskTimeRequired =
newTaskTimeRequired; }
15. public void addProjectItem(ProjectItem element){
16. if (!projectItems.contains(element)){
17. projectItems.add(element);
18. }
19. }
20. public void removeProjectItem(ProjectItem element){
21. projectItems.remove(element);
22. }
23.
24. public double getTaskTimeRequired(){ return taskTimeRequired; }
25. public Iterator getProjectItemIterator(){ return projectItems.iterator(); }
26. public double getMaterialsCost(){
27. double totalCost = 0;
28. Iterator items = getProjectItemIterator();
29. while (items.hasNext()){
30. totalCost += ((ProjectItem)items.next()).getMaterialsCost();
31. }
32. return totalCost;
33. }
34. public double getTimeRequired(){
35. double totalTime = taskTimeRequired;
36. Iterator items = getProjectItemIterator();
37. while (items.hasNext()){
38. totalTime += ((ProjectItem)items.next()).getTimeRequired();
39. }
40. return totalTime;
41. }
42. }
RunPattern creates a Deliverable object and a simple Task chain, then computes a cost estimate for each by
calling the method getCostEstimate . Each object uses the template defined in the ProjectItem class, but
applies its own methods when computing the time required and cost of materials.
Example A.132 RunPattern.java
1. public class RunPattern {
2. public static void main(String [] arguments) {
3. System.out.println("Example for the Template Method pattern");
4. System.out.println("This code demonstrates how the template method can");
5. System.out.println(" be used to define a variable implementation for a");
6. System.out.println(" common operation. In this case, the ProjectItem");
7. System.out.println(" abstract class defines the method getCostEstimate,");
8. System.out.println(" which is a combination of the cost for time and");
9. System.out.println(" materials. The two concrete subclasses used here,");
10. System.out.println(" Task and Deliverable, have different methods of");
11. System.out.println(" providing a cost estimate.");
12. System.out.println();
13.
14. System.out.println("Creating a demo Task and Deliverable");
15. System.out.println();
16. Task primaryTask = new Task("Put a JVM on the moon", "Lunar mission as part of the
JavaSpace program ;)", 240.0, 100.0);
17. primaryTask.addProjectItem(new Task("Establish ground control", "", 1000.0, 10.0));
18. primaryTask.addProjectItem(new Task("Train the Javanaughts", "", 80.0, 30.0));
Search WWH ::




Custom Search