Java Reference
In-Depth Information
RunPattern demonstrates the Interpreter functionality by creating a ContactList and running a group of
matching expressions on the elements in the list.
Example A.64 RunPattern.java
1. public class RunPattern{
2. public static void main(String [] arguments){
3. System.out.println("Example for the Interpreter pattern");
4. System.out.println("In this demonstration, the syntax defined");
5. System.out.println(" by the Interpreter can be used to search");
6. System.out.println(" among a collection of Contacts, returning");
7. System.out.println(" the subset that match the given search criteria.");
8.
9. ContactList candidates = makeContactList();
10. Context ctx = new Context();
11.
12. System.out.println("Contents of the ContactList:");
13. System.out.println(candidates);
14. System.out.println();
15.
16. Contact testContact = new ContactImpl();
17. VariableExpression varLName = new VariableExpression(testContact, "getLastName");
18. ConstantExpression constLName = new ConstantExpression("u");
19. ContainsExpression eqLName = new ContainsExpression(varLName, constLName);
20.
21. System.out.println("Contents of the search on ContactList:");
22. System.out.println("(search was contains 'u' in Lase Name)");
23. Object result = candidates.getContactsMatchingExpression(eqLName, ctx, testContact);
24. System.out.println(result);
25.
26. VariableExpression varTitle = new VariableExpression(testContact, "getTitle");
27. ConstantExpression constTitle = new ConstantExpression("LT");
28. EqualsExpression eqTitle = new EqualsExpression(varTitle, constTitle);
29.
30. System.out.println("Contents of the search on ContactList:");
31. System.out.println("(search was all LT personnel)");
32. result = candidates.getContactsMatchingExpression(eqTitle, ctx, testContact);
33. System.out.println(result);
34. System.out.println();
35.
36. VariableExpression varLastName = new VariableExpression(testContact, "getLastName");
37. ConstantExpression constLastName = new ConstantExpression("S");
38. ContainsExpression cLName = new ContainsExpression(varLastName, constLastName);
39.
40. AndExpression andExpr = new AndExpression(eqTitle, cLName);
41.
42. System.out.println("Contents of the search on ContactList:");
43. System.out.println("(search was all LT personnel with 'S' in Last Name)");
44. result = candidates.getContactsMatchingExpression(andExpr, ctx, testContact);
45. System.out.println(result);
46. }
47.
48. private static ContactList makeContactList(){
49. ContactList returnList = new ContactList();
50. returnList.addContact(new ContactImpl("James", "Kirk", "Captain", "USS
Enterprise"));
51. returnList.addContact(new ContactImpl("Mr.", "Spock", "Science Officer", "USS
Enterprise"));
52. returnList.addContact(new ContactImpl("LT", "Uhura", "LT", "USS Enterprise"));
53. returnList.addContact(new ContactImpl("LT", "Sulu", "LT", "USS Enterprise"));
54. returnList.addContact(new ContactImpl("Ensign", "Checkov", "Ensign", "USS
Enterprise"));
55. returnList.addContact(new ContactImpl("Dr.", "McCoy", "Ship's Doctor", "USS
Enterprise"));
56. returnList.addContact(new ContactImpl("Montgomery", "Scott", "LT", "USS
Enterprise"));
57. return returnList;
58. }
59. }
 
Search WWH ::




Custom Search