Java Reference
In-Depth Information
12. ObjectInputStream readIn = new ObjectInputStream(new FileInputStream(fileName));
13. returnValue = readIn.readObject();
14. readIn.close();
15. }
16. else {
17. System.err.println("Unable to locate the file " + fileName);
18. }
19. }
20. catch (ClassNotFoundException exc){
21. exc.printStackTrace();
22.
23. }
24. catch (IOException exc){
25. exc.printStackTrace();
26.
27. }
28. return returnValue;
29. }
30. }
Example A.39 RunPattern.java
1. import java.io.File;
2. import java.util.ArrayList;
3. import java.util.Iterator;
4. public class RunPattern{
5. public static void main(String [] arguments){
6. System.out.println("Example for the Chain of Responsibility pattern");
7. System.out.println();
8. System.out.println("This code uses chain of responsibility to obtain");
9. System.out.println(" the owner for a particular ProjectItem, and to");
10. System.out.println(" build up a list of project details. In each case,");
11. System.out.println(" a call to the appropriate getter method, getOwner");
12. System.out.println(" or getDetails, will pass the method call up the");
13. System.out.println(" project tree.");
14. System.out.println("For getOwner, the call will return the first non-null");
15. System.out.println(" owner field encountered. For getDetails, the method");
16. System.out.println(" will build a series of details, stopping when it");
17. System.out.println(" reaches a ProjectItem that is designated as a");
18. System.out.println(" primary task.");
19. System.out.println();
20.
21. System.out.println("Deserializing a test Project for Visitor pattern");
22. System.out.println();
23. if (!(new File("data.ser").exists())){
24. DataCreator.serialize("data.ser");
25. }
26. Project project = (Project)(DataRetriever.deserializeData("data.ser"));
27.
28. System.out.println("Retrieving Owner and details for each item in the Project");
29. System.out.println();
30. getItemInfo(project);
31. }
32.
33. private static void getItemInfo(ProjectItem item){
34. System.out.println("ProjectItem: " + item);
35. System.out.println(" Owner: " + item.getOwner());
36. System.out.println(" Details: " + item.getDetails());
37. System.out.println();
38. if (item.getProjectItems() != null){
39. Iterator subElements = item.getProjectItems().iterator();
40. while (subElements.hasNext()){
41. getItemInfo((ProjectItem)subElements.next());
42. }
43. }
44. }
45. }
 
Search WWH ::




Custom Search