Java Reference
In-Depth Information
25.
26. public String toString(){
27. return firstName + SPACE + lastName;
28. }
29. }
RunPattern provides a way to test the Flyweight. It creates ManagedList objects for addresses and contacts, then
uses common State objects to manage saving the objects to two different files.
Example A.180 RunPattern.java
1. public class RunPattern{
2. public static void main(String [] arguments) throws java.io.IOException{
3. System.out.println("Example for the Flyweight pattern");
4. System.out.println();
5. System.out.println("In this sample, State objects are shared between multiple");
6. System.out.println(" parts of the PIM. Two lists, representing a Contact list");
7. System.out.println(" and an Address Book, are used for the demonstration.");
8. System.out.println(" The State objects - CleanState and DirtyState - represent");
9. System.out.println(" the Flyweight objects in this example.");
10. System.out.println();
11.
12. System.out.println("Creating ManagedList objects to hold Contacts and Addresses");
13. ManagedList contactList = new ManagedList(Contact.class);
14. ManagedList addressList = new ManagedList(Address.class);
15. System.out.println();
16.
17. System.out.println("Printing the State for the application");
18. printPIMState();
19. System.out.println();
20.
21. System.out.println("Editing the Address and Contact lists");
22. StateFactory.getCurrentState().edit(State.CONTACTS);
23. StateFactory.getCurrentState().edit(State.ADDRESSES);
24. contactList.addItem(new ContactImpl("f", "l", "t", "o"));
25. addressList.addItem(new AddressImpl("d", "s", "c", "st", "z"));
26. System.out.println("Printing the State for the application");
27. printPIMState();
28. System.out.println();
29.
30. System.out.println("Saving the Contact list");
31. StateFactory.getCurrentState().save(new java.io.File("contacts.ser"),
contactList.getItems(), State.CONTACTS);
32. System.out.println("Printing the State for the application");
33. printPIMState();
34. System.out.println();
35.
36. System.out.println("Saving the Address list");
37. StateFactory.getCurrentState().save(new java.io.File("addresses.ser"),
addressList.getItems(), State.ADDRESSES);
38. System.out.println("Printing the State for the application");
39. printPIMState();
40. }
41.
42. private static void printPIMState(){
43. System.out.println(" Current State of the PIM: " + StateFactory.
getCurrentState().getClass());
44. System.out.println(" Object ID: " + StateFactory.getCurrentState(). hashCode());
45. System.out.println();
46. }
47. }
 
Search WWH ::




Custom Search