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

Example

Note:

For a full working example of this code example, with additional supporting classes and/or a RunPattern class, see “ Strategy ” on page 424 of the “ Full Code Examples ” appendix.

For many of the collections in the Personal Information Manager, it would be useful to be able to organize and summarize individual entries. This demonstration uses the Strategy pattern to summarize entries in a ContactList, a collection used to store Contact objects.

Example 2.41 ContactList.java

1.import java.io.Serializable;

2.import java.util.ArrayList;

3.public class ContactList implements Serializable{

4.private ArrayList contacts = new ArrayList();

5.private SummarizingStrategy summarizer;

6.

7.public ArrayList getContacts(){ return contacts; }

8.public Contact [] getContactsAsArray(){ return (Contact [])(contacts. toArray(new

Contact [1])); }

9.

10.public void setSummarizer(SummarizingStrategy newSummarizer){ summarizer =

newSummarizer; }

11. public void setContacts(ArrayList newContacts){ contacts = newContacts; }

12.

13.public void addContact(Contact element){

14.if (!contacts.contains(element)){

15. contacts.add(element);

16.}

17.}

18.public void removeContact(Contact element){

19.contacts.remove(element);

20.}

21.

22.public String summarize(){

23.return summarizer.summarize(getContactsAsArray());

24.}

25.

26.public String [] makeSummarizedList(){

27.return summarizer.makeSummarizedList(getContactsAsArray());

28.}

29.}

The ContactList has two methods, which can be used to provide summary information for the Contact objects

in the collection— summarize and make-SummarizedList. Both methods delegate to a SummarizingStrategy,

which can be set for the ContactList with the setSummarizer method.

Example 2.42 SummarizingStrategy.java

1.public interface SummarizingStrategy{

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

3.public static final String DELIMITER = ":";

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

5.public static final String SPACE = " ";

6.

7.public String summarize(Contact [] contactList);

8.public String [] makeSummarizedList(Contact [] contactList);

9.}

SummarizingStrategy is an interface that defines the two delegate methods summarize and makeSummarizedList. The interface represents the Strategy in the design pattern. In this example, two classes

represent ConcreteStrategy objects: NameSummarizer and OrganizationSummarizer. Both classes summarize

the list of contacts; however, each provides a different set of information and groups the data differently.

The NameSummarizer class returns only the names of the contacts with the last name first. The class uses an inner class as a comparator (NameComparator) to ensure that all of the Contact entries are grouped in ascending order by both last and first name.

83

Example 2.43 NameSummarizer.java

1.import java.text.Collator;

2.import java.util.Arrays;

3.import java.util.Comparator;

4.public class NameSummarizer implements SummarizingStrategy{

5.private Comparator comparator = new NameComparator();

7.public String summarize(Contact [] contactList){

8.StringBuffer product = new StringBuffer();

9.Arrays.sort(contactList, comparator);

10.for (int i = 0; i < contactList.length; i++){

11.

product.append(contactList[i].getLastName());

12.

product.append(COMMA);

13.

product.append(SPACE);

14.

product.append(contactList[i].getFirstName());

15.

product.append(EOL_STRING);

16.}

17.return product.toString();

18.}

19.

20.public String [] makeSummarizedList(Contact [] contactList){

21.Arrays.sort(contactList, comparator);

22.String [] product = new String[contactList.length];

23.for (int i = 0; i < contactList.length; i++){

24.

product[i] = contactList[i].getLastName() + COMMA + SPACE +

25.

contactList[i].getFirstName() + EOL_STRING;

26.}

27.return product;

28.}

29.

30.private class NameComparator implements Comparator{

31.private Collator textComparator = Collator.getInstance();

33.public int compare(Object o1, Object o2){

34.

Contact c1, c2;

35.

if ((o1 instanceof Contact) && (o2 instanceof Contact)){

36.

c1 = (Contact)o1;

37.

c2 = (Contact)o2;

38.

int compareResult = textComparator.compare(c1.getLastName(),c2.getLastName());

39.

if (compareResult == 0){

40.

compareResult = textComparator.compare(c1.getFirstName(),c2.getFirstName());

41.

}

42.

return compareResult;

43.

}

44.

else return textComparator.compare(o1, o2);

45.

}

46.

 

47.public boolean equals(Object o){

48. return textComparator.equals(o);

49.}

50.}

51.}

OrganizationSummarizer returns a summary with a Contact's organization, followed by their first and last name. The comparator used to order the Contact objects returns entries with ascending organization, then ascending last name.

Example 2.44 OrganizationSummarizer.java

1.import java.text.Collator;

2.import java.util.Arrays;

3.import java.util.Comparator;

4.public class OrganizationSummarizer implements SummarizingStrategy{

5.private Comparator comparator = new OrganizationComparator();

7.public String summarize(Contact [] contactList){

8.StringBuffer product = new StringBuffer();

9.Arrays.sort(contactList, comparator);

10.for (int i = 0; i < contactList.length; i++){

11.

product.append(contactList[i].getOrganization());

12.

product.append(DELIMITER);

13.

product.append(SPACE);

14.

product.append(contactList[i].getFirstName());

15.

product.append(SPACE);

16.

product.append(contactList[i].getLastName());

17.

product.append(EOL_STRING);

84

18.}

19.return product.toString();

20.}

21.

22.public String [] makeSummarizedList(Contact [] contactList){

23.Arrays.sort(contactList, comparator);

24.String [] product = new String[contactList.length];

25.for (int i = 0; i < contactList.length; i++){

26.

product[i] = contactList[i].getOrganization() + DELIMITER + SPACE +

27.

contactList[i].getFirstName() + SPACE +

28.

contactList[i].getLastName() + EOL_STRING;

29.}

30.return product;

31.}

32.

33.private class OrganizationComparator implements Comparator{

34.private Collator textComparator = Collator.getInstance();

36.public int compare(Object o1, Object o2){

37.

Contact c1, c2;

38.

if ((o1 instanceof Contact) && (o2 instanceof Contact)){

39.

c1 = (Contact)o1;

40.

c2 = (Contact)o2;

41.

int compareResult = textComparator.compare(c1.getOrganization(),

42.

c2.getOrganization());

if (compareResult == 0) {

43.

compareResult = textComparator.compare(c1.getLastName(), c2.getLastName());

44.

}

45.

return compareResult;

46.

}

47.

else return textComparator.compare(o1, o2);

48.

}

49.

 

50.public boolean equals(Object o){

51. return textComparator.equals(o);

52.}

53.}

54.}

85