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

16.selector = selectorPanel;

17.}

18.

19.public void createContact(String firstName, String lastName, String title, String

organization){

20.Contact newContact = new ContactImpl(firstName, lastName, title, organization);

21.addContact(newContact);

22.selector.addContact(newContact);

23.display.contactChanged(newContact);

24.}

25.public void updateContact(String firstName, String lastName, String title, String

organization){

26.Contact updateContact = (Contact)contacts.get(contactIndex);

27.if (updateContact != null){

28.

updateContact.setFirstName(firstName);

29.

updateContact.setLastName(lastName);

30.

updateContact.setTitle(title);

31.

updateContact.setOrganization(organization);

32.

display.contactChanged(updateContact);

33.}

34.}

35.public void selectContact(Contact contact){

36.if (contacts.contains(contact)){

37.

contactIndex = contacts.indexOf(contact);

38.

display.contactChanged(contact);

39.

editor.setContactFields(contact);

40.}

41.}

42.public Contact [] getAllContacts(){

43.return (Contact [])contacts.toArray(new Contact[1]);

44.}

45.public void addContact(Contact contact){

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

47. contacts.add(contact);

48.}

49.}

50.}

The ContactMediatorImpl interacts with each of the panels differently. For the ContactDisplayPanel, the mediator calls its contactChanged method for the create, update and select operations. For the ContactSelectorPanel, the mediator provides the list of Contacts with the getAllContacts method, receives select notifications, and adds a new Contact object to the panel when one is created. The mediator receives create and update method calls from the ContactEditorPanel, and notifies the panel of select actions from the

ContactSelectorPanel.

Memento

Also known as Token, Snapshot

Pattern Properties

Type: Behavioral

Level: Object

Purpose

To preserve a “snapshot” of an object’s state, so that the object can return to its original state without having to reveal its content to the rest of the world.

Introduction

Every application has objects that need to preserve information beyond their lifespan. Often, this relates to shared data, but what if the private data of an object needs to be preserved? Sending the data to another object is a bad idea, since it goes against the rules of encapsulation. If you sent data to other objects, they would be able to read or, even worse, modify the data.

It's like going to a national park where they preserve the moose. The object whose data is being saved is that moose. You’re not allowed to take a moose home with you, but postcards and moose t-shirts are available at the national park gift shop.

63