Java Reference
In-Depth Information
24. public void setOrganization(String newOrganization){ organization = newOrganization; }
25.
26. public String toString(){
27. return firstName + SPACE + lastName;
28. }
29. }
The RunPattern class creates several ProjectItems and prints out their String values. Next, it creates several
Decorators , associates them with one of the Task objects by calling the setProjectItem methods, and shows
the String value of the newly decorated Task .
Example A.162 RunPattern.java
1. import java.io.File;
2. public class RunPattern{
3. public static void main(String [] arguments){
4. System.out.println("Example for the Decorator pattern");
5. System.out.println();
6. System.out.println("This demonstration will show how Decorator classes can be
used");
7. System.out.println(" to extend the basic functionality of ProjectItems. The Task
and");
8. System.out.println(" Deliverable classes provide the basic ProjectItems, and
their");
9. System.out.println(" functionality will be extended by adding subclasses of the");
10. System.out.println(" abstract class ProjectDecorator.");
11. System.out.println();
12. System.out.println("Note that the toString method has been overridden for all
ProjectItems,");
13 System.out.println(" to more effectively show how Decorators are
associated with their");
14. System.out.println(" ProjectItems.");
15. System.out.println();
16.
17. System.out.println("Creating ProjectItems.");
18. Contact contact1 = new ContactImpl("Simone", "Roberto", "Head Researcher and Chief
Archivist", "Institute for Advanced (Java) Studies");
19. Task task1 = new Task("Perform months of diligent research", contact1, 20.0);
20. Task task2 = new Task("Obtain grant from World Java Foundation", contact1, 40.0);
21. Deliverable deliverable1 = new Deliverable("Java History", "Comprehensive history
of the design of all Java APIs", contact1);
22. System.out.println("ProjectItem objects created. Results:");
23. System.out.println(task1);
24. System.out.println(task2);
25. System.out.println(deliverable1);
26. System.out.println();
27.
28. System.out.println("Creating decorators");
29. ProjectDecorator decorator1 = new SupportedProjectItem(new File(" JavaHistory.txt"));
30. ProjectDecorator decorator2 = new DependentProjectItem(task2);
31. System.out.println("Decorators created. Adding decorators to the first task");
32. decorator1.setProjectItem(task1);
33. decorator2.setProjectItem(decorator1);
34. System.out.println();
35. System.out.println("Decorators added. Results");
36. System.out.println(decorator2);
37.
38. System.out.println("");
39. }
40. }
 
Search WWH ::




Custom Search