Java Reference
In-Depth Information
a terminal node. This means that getDetails returns a series of Strings representing all the details for a
particular Task chain.
Support classes for the example include the Contact interface and ContactImpl class, which are used by Project and
Task to define an owner.
Example A.35 Contact.java
1. import java.io.Serializable;
2. public interface Contact extends Serializable{
3. public static final String SPACE = " ";
4. public String getFirstName();
5. public String getLastName();
6. public String getTitle();
7. public String getOrganization();
8.
9. public void setFirstName(String newFirstName);
10. public void setLastName(String newLastName);
11. public void setTitle(String newTitle);
12. public void setOrganization(String newOrganization);
13. }
Example A.36 ContactImpl.java
1. public class ContactImpl implements Contact{
2. private String firstName;
3. private String lastName;
4. private String title;
5. private String organization;
6.
7. public ContactImpl(){}
8. public ContactImpl(String newFirstName, String newLastName,
9. String newTitle, String newOrganization){
10. firstName = newFirstName;
11. lastName = newLastName;
12. title = newTitle;
13. organization = newOrganization;
14. }
15.
16. public String getFirstName(){ return firstName; }
17. public String getLastName(){ return lastName; }
18. public String getTitle(){ return title; }
19. public String getOrganization(){ return organization; }
20.
21. public void setFirstName(String newFirstName){ firstName = newFirstName; }
22. public void setLastName(String newLastName){ lastName = newLastName; }
23. public void setTitle(String newTitle){ title = newTitle; }
24. public void setOrganization(String newOrganization){ organization = newOrganization; }
25.
26. public String toString(){
27. return firstName + SPACE + lastName;
28. }
29. }
The DataCreator class provide support classes to generate data and serialize it to a file, while the
DataRetriever class retrieves the data for use in the example. The RunPattern class coordinates between the
other classes in the example, getting a project, then retrieving the owner and details for each Task and for the
Project itself.
Example A.37 DataCreator.java
1. import java.io.Serializable;
2. import java.io.ObjectOutputStream;
3. import java.io.FileOutputStream;
4. import java.io.IOException;
5.
6. public class DataCreator{
7. private static final String DEFAULT_FILE = "data.ser";
8.
9. public static void main(String [] args){
10. String fileName;
11. if (args.length == 1){
12. fileName = args[0];
13. }
14. else{
15. fileName = DEFAULT_FILE;
Search WWH ::




Custom Search