Java Reference
In-Depth Information
ContactMediatorImpl is the implementer of ContactMediator . It maintains a collection of Contacts, and
methods that notify the panels of changes within the GUI.
Example A.79 ContactMediatorImpl.java
1. import java.util.ArrayList;
2. public class ContactMediatorImpl implements ContactMediator{
3. private ContactDisplayPanel display;
4. private ContactEditorPanel editor;
5. private ContactSelectorPanel selector;
6. private ArrayList contacts = new ArrayList();
7. private int contactIndex;
8.
9. public void setContactDisplayPanel(ContactDisplayPanel displayPanel){
10. display = displayPanel;
11. }
12. public void setContactEditorPanel(ContactEditorPanel editorPanel){
13. editor = editorPanel;
14. }
15. public void setContactSelectorPanel(ContactSelectorPanel selectorPanel){
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 .
Contact and ContactImpl define the business class used in this example.
Example A.80 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();
Search WWH ::




Custom Search