Java Reference
In-Depth Information
20. }catch (IOException exc){
21. exc.printStackTrace();
22. }
23. return returnValue;
24. }
25. }
RunPattern coordinates this example by loading the sample data, then calling the ListPrinter method
printToDoListCollection to print all lists and their elements.
Example A.73 RunPattern.java
1. import java.io.File;
2. import java.io.IOException;
3. public class RunPattern{
4. public static void main(String [] arguments){
5. System.out.println("Example for the Iterator pattern");
6. System.out.println(" This code sample demonstrates how an Iterator can enforce");
7. System.out.println(" uniformity of processing for different collection types.");
8. System.out.println(" In this case, there are two classes, ToDoListImpl and");
9. System.out.println(" ToDoListCollectionImpl, that have different storage needs.");
10. System.out.println(" ToDoListImpl uses an ArrayList to store its elements in");
11. System.out.println(" ordered form. The ToDoListCollectionImpl uses a HashMap,");
12. System.out.println(" since it must differentiate between ToDoListImpl objects by");
13. System.out.println(" their String identifiers.");
14. System.out.println();
15. System.out.println("Although the two classes use different underlying collections,");
16. System.out.println(" the ListPrinter class can use the Iterator produced by each");
17. System.out.println(" to print out a set of list contents.");
18. System.out.println();
19.
20. if (!(new File("data.ser").exists())){
21. DataCreator.serialize("data.ser");
22. }
23. ToDoListCollection lists = (ToDoListCollection)(DataRetriever.
deserializeData("data.ser"));
24.
25. System.out.println("Lists retrieved. Printing out contents using the Iterator");
26. System.out.println();
27. ListPrinter.printToDoListCollection(lists, System.out);
28. }
29. }
 
Search WWH ::




Custom Search