Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Memento

Almost all parts of the Personal Information Manager keep some kind of state. These states can be saved by applying the Memento pattern, as this example with an address book will demonstrate. The AddressBook class represents a collection of addresses, a natural candidate for keeping a record of state.

Example A.83 AddressBook.java

1.import java.util.ArrayList;

2.public class AddressBook{

3.private ArrayList contacts = new ArrayList();

5.public Object getMemento(){

6.return new AddressBookMemento(contacts);

7.}

8.public void setMemento(Object object){

9.if (object instanceof AddressBookMemento){

10. AddressBookMemento memento = (AddressBookMemento)object;

11. contacts = memento.state;

12.}

13.}

15.private class AddressBookMemento{

16.private ArrayList state;

17.

18.private AddressBookMemento(ArrayList contacts){

19. this.state = contacts;

20.}

21.}

23.public AddressBook(){ }

24.public AddressBook(ArrayList newContacts){

25.contacts = newContacts;

26.}

27.

28.public void addContact(Contact contact){

29.if (!contacts.contains(contact)){

30. contacts.add(contact);

31.}

32.}

33.public void removeContact(Contact contact){

34.contacts.remove(contact);

35.}

36.public void removeAllContacts(){

37.contacts = new ArrayList();

38.}

39.public ArrayList getContacts(){

40.return contacts;

41.}

42.public String toString(){

43.return contacts.toString();

44.}

45.}

The inner class of AddressBook, AddressBookMemento, is used to save the state of an AddressBook, which in this case is represented by the internal ArrayList of Address objects. The memento object can be accessed by

using the AddressBook methods getMemento and setMemento. Note that AddressBookMemento is a private

inner class and that it has only a private constructor. This ensures that, even if the memento object is saved somewhere outside of an AddressBook object, no other object will be able to use the object or modify its state. This is consistent with the role of the Memento pattern: producing an object to maintain a snapshot of state that cannot be modified by other objects in a system.

Support classes used in this example provide business objects for the contacts stored in the AddressBook, and their associated addresses. The Address and Contact interfaces define the behavior expected of these business objects, while the AddressImpl and ContactImpl classes implement the required behavior.

Example A.84 Address.java

1.import java.io.Serializable;

2.public interface Address extends Serializable{

3.public static final String EOL_STRING = System.getProperty("line.separator");

4.public static final String SPACE = " ";

5.public static final String COMMA = ",";

262

6.public String getType();

7.public String getDescription();

8.public String getStreet();

9.public String getCity();

10.public String getState();

11.public String getZipCode();

13.public void setType(String newType);

14.public void setDescription(String newDescription);

15.public void setStreet(String newStreet);

16.public void setCity(String newCity);

17.public void setState(String newState);

18.public void setZipCode(String newZip);

19.}

Example A.85 AddressImpl.java

1.public class AddressImpl implements Address{

2.private String type;

3.private String description;

4.private String street;

5.private String city;

6.private String state;

7.private String zipCode;

8.

9.public AddressImpl(){ }

10.public AddressImpl(String newDescription, String newStreet,

11.String newCity, String newState, String newZipCode){

12.description = newDescription;

13.street = newStreet;

14.city = newCity;

15.state = newState;

16.zipCode = newZipCode;

17.}

18.

19.public String getType(){ return type; }

20.public String getDescription(){ return description; }

21.public String getStreet(){ return street; }

22.public String getCity(){ return city; }

23.public String getState(){ return state; }

24.public String getZipCode(){ return zipCode; }

25.

26.public void setType(String newType){ type = newType; }

27.public void setDescription(String newDescription){ description = newDescription; }

28.public void setStreet(String newStreet){ street = newStreet; }

29.public void setCity(String newCity){ city = newCity; }

30.public void setState(String newState){ state = newState; }

31.public void setZipCode(String newZip){ zipCode = newZip; }

32.

33.public String toString(){

34.return street + EOL_STRING + city + COMMA + SPACE +

35. state + SPACE + zipCode + EOL_STRING;

36.}

37.}

Example A.86 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.87 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.private Address address;

263

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()));

264

36.book.addContact(new ContactImpl("Alexander", "Dane", "", "Actor's Guild", new

AddressImpl()));

37.book.addContact(new ContactImpl("Fred", "Kwan", "", "Actor's Guild", new

AddressImpl()));

38.

39.System.out.println("New Contacts added. Current Contact list:");

40.System.out.println(book);

41.System.out.println();

42.System.out.println("Using the Memento object to restore the AddressBook");

43.System.out.println(" to its original state.");

44.book.setMemento(memento);

45.System.out.println("AddressBook restored. Current Contact list:");

46.System.out.println(book);

47.

48.}

49.}

265