Java Reference
In-Depth Information
30. return totalTime;
31. }
32.
33. public void setName(String newName){ name = newName; }
34. public void setDetails(String newDetails){ details = newDetails; }
35. public void setOwner(Contact newOwner){ owner = newOwner; }
36. public void setTimeRequired(double newTimeRequired){ timeRequired = newTimeRequired; }
37.
38. public void addProjectItem(ProjectItem element){
39. if (!projectItems.contains(element)){
40. projectItems.add(element);
41. }
42. }
43. public void removeProjectItem(ProjectItem element){
44. projectItems.remove(element);
45. }
46. }
The
getTimeRequired
method shows how the Composite pattern runs. To get the time estimate for any part of
the project, you simply call the method
getTimeRequired
for a
Project
or
Task
object. This method behaves
differently depending on the method implementer:
Deliverable:
Return 0.
Project
or
Task:
Return the sum of the time required for the object plus the results of calling the
getTimeRequired
method for all
ProjectItems
associated with this node.
The
Contact
interface and
ContactImpl
class provide support code to represent the owner of a task or
deliverable.
Example A.149
Contact.java
1. import java.io.Serializable;
2. public interface Contact extends Serializable{
3. public static final String SPACE = " ";
4. public String getFirstName();
5. public String getLastName();
6. public String getTitle();
7. public String getOrganization();
8.
9. public void setFirstName(String newFirstName);
10. public void setLastName(String newLastName);
11. public void setTitle(String newTitle);
12. public void setOrganization(String newOrganization);
13. }
Example A.150
ContactImpl.java
1. public class ContactImpl implements Contact{
2. private String firstName;
3. private String lastName;
4. private String title;
5. private String organization;
6.
7. public ContactImpl(){}
8. public ContactImpl(String newFirstName, String newLastName,
9. String newTitle, String newOrganization){
10. firstName = newFirstName;
11. lastName = newLastName;
12. title = newTitle;
13. organization = newOrganization;
14. }
15.
16. public String getFirstName(){ return firstName; }
17. public String getLastName(){ return lastName; }
18. public String getTitle(){ return title; }
19. public String getOrganization(){ return organization; }
20.
21. public void setFirstName(String newFirstName){ firstName = newFirstName; }
22. public void setLastName(String newLastName){ lastName = newLastName; }
23. public void setTitle(String newTitle){ title = newTitle; }
24. public void setOrganization(String newOrganization){ organization = newOrganization; }
25.
26. public String toString() {
27. return firstName + SPACE + lastName;