Java Reference
In-Depth Information
3. public class SupportedProjectItem extends ProjectDecorator{
4. private ArrayList supportingDocuments = new ArrayList();
5.
6. public SupportedProjectItem(){ }
7. public SupportedProjectItem(File newSupportingDocument){
8. addSupportingDocument(newSupportingDocument);
9. }
10.
11. public ArrayList getSupportingDocuments(){
12. return supportingDocuments;
13. }
14.
15. public void addSupportingDocument(File document){
16. if (!supportingDocuments.contains(document)){
17. supportingDocuments.add(document);
18. }
19. }
20.
21. public void removeSupportingDocument(File document){
22. supportingDocuments.remove(document);
23. }
24.
25. public String toString(){
26. return getProjectItem().toString() + EOL_STRING
27. + "\tSupporting Documents: " + supportingDocuments;
28. }
29. }
The benefit of defining additional capabilities in this way is that it is easy to create project items that have a
combination of capabilities. Using these classes, you can make a simple task that depends on another project item,
or a task with supporting documents. You can even chain Decorators together and create a task that depends on
another task and has supporting documents. This flexibility is a key strength of the Decorator pattern.
In this example, the Contact interface and its implementer ContactImpl provide support for an owner of a Task
or Deliverable .
Example A.160 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.161 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; }
Search WWH ::




Custom Search