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

Adapter (page 142) – An Adapter provides a front interface to a specific object, as does the Proxy pattern. However, the Proxy provides the same interface as the object, and the Adapter provides a different interface.

HOPP (page 189) – The HOPP pattern can use the Proxy pattern for the communication between the two distributed halves of the HOPP.

Business Delegate [CJ2EEP] – The Business Delegate pattern can be used as a Proxy. The Business Delegate can be a local representative of the Business tier.

Example

Note:

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

An address book grows tremendously over a period of time, since it stores all professional and social contacts. In addition, users don't need the address book every time they use the PIM. They do need some kind of address book placeholder to act as a starting point for them to use for graphical purposes, however. This example uses the Proxy pattern to represent the address book.

AddressBook defines the interface for accessing the PIM address book. At the very least, it needs to have the ability to add new contacts and to retrieve and store addresses.

Example 3.32 AddressBook.java

1.import java.io.IOException;

2.import java.util.ArrayList;

3.public interface AddressBook {

4.public void add(Address address);

5.public ArrayList getAllAddresses();

6.public Address getAddress(String description);

8.public void open();

9.public void save();

10.}

Retrieving the data for the address book might be very time-consuming, given the incredible popularity of the users. Therefore, the proxy should delay creation of the real address book for as long as possible. The proxy, represented by AddressBookProxy, has the responsibility for creating the address book— but only when absolutely necessary.

Example 3.33 AddressBookProxy.java

1.import java.io.File;

2.import java.io.IOException;

3.import java.util.ArrayList;

4.import java.util.Iterator;

5.public class AddressBookProxy implements AddressBook{

6.private File file;

7.private AddressBookImpl addressBook;

8.private ArrayList localAddresses = new ArrayList();

10.public AddressBookProxy(String filename){

11.file = new File(filename);

12.}

13.

14.public void open(){

15.addressBook = new AddressBookImpl(file);

16.Iterator addressIterator = localAddresses.iterator();

17.while (addressIterator.hasNext()){

18. addressBook.add((Address)addressIterator.next());

19.}

20.}

22.public void save(){

23.if (addressBook != null){

24. addressBook.save();

25.} else if (!localAddresses.isEmpty()){

26. open();

136

27. addressBook.save();

28.}

29.}

31.public ArrayList getAllAddresses(){

32.if (addressBook == null) {

33. open();

34.}

35.return addressBook.getAllAddresses();

36.}

37.

38.public Address getAddress(String description){

39.if (!localAddresses.isEmpty()){

40.

Iterator addressIterator = localAddresses.iterator();

41.

while (addressIterator.hasNext()){

42.

AddressImpl address = (AddressImpl)addressIterator.next();

43.

if (address.getDescription().equalsIgnoreCase(description)){

44.

return address;

45.

}

46.

}

47.}

48.if (addressBook == null){

49. open();

50.}

51.return addressBook.getAddress(description);

52.}

53.

54.public void add(Address address){

55.if (addressBook != null){

56. addressBook.add(address);

57.} else if (!localAddresses.contains(address)){

58. localAddresses.add(address);

59.}

60.}

61.}

Note that the AddressBookProxy has its own ArrayList for addresses. If the user adds an address by calling the add method, the proxy can use its internal address book without using the real address book.

The AddressBookImpl class represents the real address book for a user. It is associated with a file that stores an ArrayList with all the user's addresses. AddressBookProxy would create an AddressBookImpl object only when it is needed—when a user called the method getAllAddresses, for example.

Example 3.34 AddressBookImpl.java

1.import java.io.File;

2.import java.io.IOException;

3.import java.util.ArrayList;

4.import java.util.Iterator;

5.public class AddressBookImpl implements AddressBook {

6.private File file;

7.private ArrayList addresses = new ArrayList();

8.

9.public AddressBookImpl(File newFile) {

10.file = newFile;

11.open();

12.}

13.

14. public ArrayList getAllAddresses(){ return addresses; }

15.

16.public Address getAddress(String description){

17.Iterator addressIterator = addresses.iterator();

18.while (addressIterator.hasNext()){

19.

AddressImpl address = (AddressImpl)addressIterator.next();

20.

if (address.getDescription().equalsIgnoreCase(description)){

21.

return address;

22.

}

23.}

24.return null;

25.}

26.

27.public void add(Address address) {

28.if (!addresses.contains(address)){

29. addresses.add(address);

30.}

31.}

137

32.

33.public void open(){

34.addresses = (ArrayList)FileLoader.loadData(file);

35.}

36.

37.public void save(){

38.FileLoader.storeData(file, addresses);

39.}

40.}

138

Chapter 4. System Patterns

Introduction to System Patterns

System patterns are the most diverse of the four pattern types. They embrace your application at its most abstract, architectural level. System patterns can apply to major processes within an application, or even between applications.

System patterns include the following:

Model-View-Controller (MVC) – To divide a component or subsystem into three logical parts—model, view, and controller—making it easier to modify or customize each part.

Session – To provide a way for servers in distributed systems to distinguish among clients, allowing applications to associate state with the client-server communication.

Worker Thread – To improve throughput and minimize average latency.

Callback – To allow a client to register with a server for extended operations. This enables the server to notify the client when the operation has been completed.

Successive Update – To provide a way for clients to receive updates from a server on an ongoing basis. The updates generally reflect a change in server data, a new or updated resource, or a change in the state of the business model.

Router – To decouple multiple sources of information from the targets of that information.

Transaction – To group a collection of methods so that they either all succeed or they all fail collectively.

139