Java Reference
In-Depth Information
36. public String toString(){
37. return contacts.toString();
38. }
39. }
With the Expression hierarchy and the ContactList , it is possible to perform database-like queries for the
Contacts in a ContactList . For example, you could search for all those Contacts with a title containing the
characters “Java” by doing the following:
Create a ConstantExpression with the string “Java”.
Create a VariableExpression with the target object and the string “ getTitle ”.
Create a ContainsExpression with the VariableExpression as the first argument and the
ConstantExpression as the second.
Pass the ContainsExpression into a ContactList object's getContactsMatchingExpression method.
Contact and its implementer ContactImpl represent the business objects to be evaluated in this example.
Example A.62 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.63 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. }
This code shows how the Interpreter could be used to search among a set of Contacts in a structure like an
address book. Recognize, however, that the Expressions could be used with any other classes, providing search
functionality for any of the PIM business objects.
Search WWH ::




Custom Search