Java Reference
In-Depth Information
7.
8. public ContactImpl(){}
9. public ContactImpl(String newFirstName, String newLastName,
10. String newTitle, String newOrganization, Address newAddress){
11. firstName = newFirstName;
12. lastName = newLastName;
13. title = newTitle;
14. organization = newOrganization;
15. address = newAddress;
16. }
17.
18. public String getFirstName(){ return firstName; }
19. public String getLastName(){ return lastName; }
20. public String getTitle(){ return title; }
21. public String getOrganization(){ return organization; }
22. public Address getAddress(){ return address; }
23.
24. public void setFirstName(String newFirstName){ firstName = newFirstName; }
25. public void setLastName(String newLastName){ lastName = newLastName; }
26. public void setTitle(String newTitle){ title = newTitle; }
27. public void setOrganization(String newOrganization){ organization = newOrganization; }
28. public void setAddress(Address newAddress){ address = newAddress; }
29.
30. public String toString(){
31. return firstName + " " + lastName;
32. }
33. }
The RunPattern class demonstrates the use of the Memento by creating an address book with an initial set of
people. Next, RunPattern saves the state of this group of contacts to an AddressBookMemento object and creates
a different set of people. Finally, RunPattern restores the address book to its original state by calling its
setMemento method.
Example A.88 RunPattern.java
1. public class RunPattern{
2. public static void main(String [] arguments){
3. System.out.println("Example for the Memento pattern");
4. System.out.println();
5. System.out.println("This example will use the AddressBook to demonstrate");
6. System.out.println(" how a Memento can be used to save and restore state.");
7. System.out.println("The AddressBook has an inner class, AddressBookMemento,");
8. System.out.println(" that is used to store the AddressBook state... in this");
9. System.out.println(" case, its internal list of contacts.");
10. System.out.println();
11.
12. System.out.println("Creating the AddressBook");
13. AddressBook book = new AddressBook();
14.
15. System.out.println("Adding Contact entries for the AddressBook");
16. book.addContact(new ContactImpl("Peter", "Taggart", "Commander", "NSEA Protector",
new AddressImpl()));
17. book.addContact(new ContactImpl("Tawny", "Madison", "Lieutenant", "NSEA Protector",
new AddressImpl()));
18. book.addContact(new ContactImpl("Dr.", "Lazarus", "Dr.", "NSEA Protector", new
AddressImpl()));
19. book.addContact(new ContactImpl("Tech Sargent", "Chen", "Tech Sargent", "NSEA
Protector", new AddressImpl()));
20.
21. System.out.println("Contacts added. Current Contact list:");
22. System.out.println(book);
23. System.out.println();
24.
25. System.out.println("Creating a Memento for the address book");
26. Object memento = book.getMemento();
27. System.out.println("Now that a Memento exists, it can be used to restore");
28. System.out.println(" the state of this AddressBook object, or to set the");
29. System.out.println(" state of a new AddressBook.");
30. System.out.println();
31.
32. System.out.println("Creating new entries for the AddressBook");
33. book.removeAllContacts();
34. book.addContact(new ContactImpl("Jason", "Nesmith", "", "Actor's Guild", new
AddressImpl()));
35. book.addContact(new ContactImpl("Gwen", "DeMarco", "", "Actor's Guild", new
AddressImpl()));
Search WWH ::




Custom Search