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

Bridge (page 150) – Although the Adapter and the Bridge pattern are very similar, their intent is different. The Bridge pattern separates the abstraction and the implementation of a component and allows each to be changed independently. The Adapter pattern allows using an otherwise incompatible existing object.

Decorator (page 166) – The Adapter pattern is intended to change the interface of an object, but keep the same functionality. The Decorator leaves the interface of the object the same but enhances its functionality.

Proxy (page 197) – Both the Adapter pattern and the Proxy pattern provide a front interface to an object. The difference is that the Adapter pattern provides a different interface and the Proxy pattern provides the same interface as the object.

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.

The Business Delegate can also operate as an Adapter for otherwise incompatible systems.

Example

In this example, the PIM uses an API provided by a foreign source. Two files represent the interface into a purchased set of classes intended to represent contacts. The basic operations are defined in the interface called

Chovnatlh.

Example 3.1 Chovnatlh.java

1.public interface Chovnatlh{

2.public String tlhapWa$DIchPong();

3.public String tlhapQavPong();

4.public String tlhapPatlh();

5.public String tlhapGhom();

6.

public void cherWa$DIchPong(String chu$wa$DIchPong);

7.

8.

public void cherQavPong(String chu$QavPong);

9.

public void cherPatlh(String chu$patlh);

 

10.

public void cherGhom(String chu$ghom);

Y

11.

}

 

 

 

 

 

 

 

 

L

 

 

 

 

 

 

 

 

 

 

 

F

The implementation for these methods is provided in the associated class, ChovnatlhImpl.

 

 

 

 

 

M

 

Example 3.2

ChovnatlhImpl.java

 

A

 

E

 

 

1.

// pong = name

 

 

T

 

 

 

2.

// wa'DIch = first

 

 

 

 

3.

// Qav = last

 

 

 

 

4.// patlh = rank (title)

5.// ghom = group (organization)

6.// tlhap = take (get)

7.// cher = set up (set)

8.// chu' = new

9.// chovnatlh = specimen (contact)

11.public class ChovnatlhImpl implements Chovnatlh{

12.private String wa$DIchPong;

13.private String QavPong;

14.private String patlh;

15.private String ghom;

16.

17.public ChovnatlhImpl(){ }

18.public ChovnatlhImpl(String chu$wa$DIchPong, String chu$QavPong,

19.String chu$patlh, String chu$ghom){

20. wa$DIchPong = chu$wa$DIchPong; 21. QavPong = chu$QavPong;

22. patlh = chu$patlh;

23. ghom = chu$ghom;

24. }

25.

26.public String tlhapWa$DIchPong(){ return wa$DIchPong; }

27.public String tlhapQavPong(){ return QavPong; }

28.public String tlhapPatlh(){ return patlh; }

29.public String tlhapGhom(){ return ghom; }

30.

public void cherWa$DIchPong(String

wa$DIchPong = chu$wa$DIchPong; }

31.

32.public void cherQavPong(StringTEAMchuFLY$QavPong){PRESENTSQavPong = chu$QavPong; }

33.public void cherPatlh(String chu$patlh){ patlh = chu$patlh; }

101

34. public void cherGhom(String chu$ghom){ ghom = chu$ghom; } 35.

36.public String toString(){

37.return wa$DIchPong + " " + QavPong + ": " + patlh + ", " + ghom;

38.}

39.}

With help from a translator, it is possible to match the methods to those found in the Contact interface. The ContactAdapter class performs this task by using a variable to hold an internal ChovnatlhImpl object. This object manages the information required to hold the Contact information: name, title, and organization.

Example 3.3 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 3.4 ContactAdapter.java

1.public class ContactAdapter implements Contact{

2.private Chovnatlh contact;

3.

4.public ContactAdapter(){

5.contact = new ChovnatlhImpl();

6.}

7.public ContactAdapter(Chovnatlh newContact){

8.contact = newContact;

9.}

10.

11.public String getFirstName(){

12.return contact.tlhapWa$DIchPong();

13.}

14.public String getLastName(){

15.return contact.tlhapQavPong();

16.}

17.public String getTitle(){

18.return contact.tlhapPatlh();

19.}

20.public String getOrganization(){

21.return contact.tlhapGhom();

22.}

23.

24.public void setContact(Chovnatlh newContact){

25.contact = newContact;

26.}

27.public void setFirstName(String newFirstName){

28.contact.cherWa$DIchPong(newFirstName);

29.}

30.public void setLastName(String newLastName){

31.contact.cherQavPong(newLastName);

32.}

33.public void setTitle(String newTitle){

34.contact.cherPatlh(newTitle);

35.}

36.public void setOrganization(String newOrganization){

37.contact.cherGhom(newOrganization);

38.}

39.

40.public String toString(){

41.return contact.toString();

42.}

43.}

102