Java Reference
In-Depth Information
28. }
29. }
This example uses a small demonstration project to illustrate the Command pattern. To simplify the task of
managing a stored copy of the project information, the DataCreator class creates a sample project and serializes
it to a file.
Example A.151 DataCreator.java
1. import java.io.Serializable;
2. import java.io.ObjectOutputStream;
3. import java.io.FileOutputStream;
4. import java.io.IOException;
5. public class DataCreator {
6. private static final String DEFAULT_FILE = "data.ser";
7.
8. public static void main(String [] args){
9. String fileName;
10. if (args.length == 1){
11. fileName = args[0];
12. }
13. else{
14. fileName = DEFAULT_FILE;
15. }
16. serialize(fileName);
17. }
18.
19. public static void serialize(String fileName){
20. try{
21. serializeToFile(createData(), fileName);
22. }
23. catch (IOException exc){
24. exc.printStackTrace();
25. }
26. }
27.
28. private static Serializable createData(){
29. Contact contact1 = new ContactImpl("Dennis", "Moore", "Managing Director", "Highway
Man, LTD");
30. Contact contact2 = new ContactImpl("Joseph", "Mongolfier", "High Flyer", "Lighter
than Air Productions");
31. Contact contact3 = new ContactImpl("Erik", "Njoll", "Nomad without Portfolio",
"Nordic Trek, Inc.");
32. Contact contact4 = new ContactImpl("Lemming", "", "Principal Investigator", "BDA");
33.
34. Project project = new Project("IslandParadise", "Acquire a personal island
paradise");
35. Deliverable deliverable1 = new Deliverable("Island Paradise", "", contact1);
36. Task task1 = new Task("Fortune", "Acquire a small fortune", contact4, 11.0);
37. Task task2 = new Task("Isle", "Locate an island for sale", contact2, 7.5);
38. Task task3 = new Task("Name", "Decide on a name for the island", contact3, 3.2);
39. project.addProjectItem(deliverable1);
40. project.addProjectItem(task1);
41. project.addProjectItem(task2);
42. project.addProjectItem(task3);
43.
44. Deliverable deliverable11 = new Deliverable("$1,000,000", "(total net worth after
taxes)", contact1);
45. Task task11 = new Task("Fortune1", "Use psychic hotline to predict winning lottery
numbers", contact4, 2.5);
46. Task task12 = new Task("Fortune2", "Invest winnings to ensure 50% annual interest",
contact1, 14.0);
47. task1.addProjectItem(task11);
48. task1.addProjectItem(task12);
49. task1.addProjectItem(deliverable11);
50.
51. Task task21 = new Task("Isle1", "Research whether climate is better in the Atlantic
or Pacific", contact1, 1.8);
52. Task task22 = new Task("Isle2", "Locate an island for auction on EBay", contact4,
5.0);
53. Task task23 = new Task("Isle2a", "Negotiate for sale of the island", contact3, 17.5);
54. task2.addProjectItem(task21);
55. task2.addProjectItem(task22);
56. task2.addProjectItem(task23);
57.
58. Deliverable deliverable31 = new Deliverable("Island Name", "", contact1);
59. task3.addProjectItem(deliverable31);
Search WWH ::




Custom Search