Java Reference
In-Depth Information
60. return project;
61. }
62.
63. private static void serializeToFile(Serializable content, String fileName) throws
IOException {
64. ObjectOutputStream serOut = new ObjectOutputStream(new FileOutputStream(fileName));
65. serOut.writeObject(content);
66. serOut.close();
67. }
68. }
The DataRetriever class provides a resource to deserialize an object from a file with the deserializeData
method.
Example A.152 DataRetriever.java
1. import java.io.File;
2. import java.io.FileInputStream;
3. import java.io.IOException;
4. import java.io.ObjectInputStream;
5.
6. public class DataRetriever{
7. public static Object deserializeData(String fileName){
8. Object returnValue = null;
9. try{
10. File inputFile = new File(fileName);
11. if (inputFile.exists() && inputFile.isFile()){
12. ObjectInputStream readIn = new ObjectInputStream(new FileInputStream(fileName));
13. returnValue = readIn.readObject();
14. readIn.close();
15. }else{
16. System.err.println("Unable to locate the file " + fileName);
17. }
18. }catch (ClassNotFoundException exc){
19. exc.printStackTrace();
20.
21. }catch (IOException exc){
22. exc.printStackTrace();
23. }
24. return returnValue;
25. }
26. }
The RunPattern class uses DataRetriever to deserialize the project, then calls the getTimeRequired method to
calculate the time requirements for the entire project.
Example A.153 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 Composite pattern");
5. System.out.println();
6. System.out.println("This code sample will propagate a method call throughout");
7. System.out.println(" a tree structure. The tree represents a project, and is");
8. System.out.println(" composed of three kinds of ProjectItems - Project, Task,");
9. System.out.println(" and Deliverable. Of these three classes, Project and Task");
10. System.out.println(" can store an ArrayList of ProjectItems. This means that");
11. System.out.println(" they can act as branch nodes for our tree. The Deliverable");
12. System.out.println(" is a terminal node, since it cannot hold any ProjectItems.");
13. System.out.println();
14. System.out.println("In this example, the method defined by ProjectItem,");
15. System.out.println(" getTimeRequired, provides the method to demonstrate the");
16. System.out.println(" pattern. For branch nodes, the method will be passed on");
17. System.out.println(" to the children. For terminal nodes (Deliverables), a");
18. System.out.println(" single value will be returned.");
19. System.out.println();
20. System.out.println("Note that it is possible to make this method call ANYWHERE");
21. System.out.println(" in the tree, since all classes implement the getTimeRequired");
22. System.out.println(" method. This means that you are able to calculate the time");
23. System.out.println(" required to complete the whole project OR any part of it.");
24. System.out.println();
25.
26. System.out.println("Deserializing a test Project for the Composite pattern");
27. System.out.println();
28. if (!(new File("data.ser").exists())){
29. DataCreator.serialize("data.ser");
Search WWH ::




Custom Search