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

Description

Sometimes you’d like to use a class in a new framework without recoding it to match the new environment. In such cases, you can design an Adaptee class to act as a translator. This class receives calls from the environment and modifies the calls to be compatible with the Adaptee class.

Typical environments where an Adapter can be useful include applications that support plug-in behavior like graphics, text, or media editors. Web browsers are another environment where the Adapter can be useful. Applications involving internationalization (language conversion, for example) might benefit from Adapters, as well as those that use components (such as JavaBeans™) that are added on the fly.

A real-world example of the Adapter is a foreign language phrase book. The phrase book translates common expressions (messages) from one language to another, enabling two otherwise incompatible individuals to communicate with each other. This assumes, of course, that the phrase book translates the messages properly based on the context: “I need to buy some matches,” for example, might not map correctly to “My hovercraft is full of eels” in all circumstances.

Implementation

The Adapter class diagram interface is shown in Figure 3.1.

Figure 3.1. Adapter class diagram interface

Implementing the Adapter pattern requires the following:

Framework – The Framework uses the Adapter. It either constructs the ConcreteAdapter or it gets set somewhere.

Adapter – The interface that defines the methods the Framework uses.

ConcreteAdapter – An implementation of the Adapter interface. It keeps a reference to the Adaptee and translates the method calls from the Framework into method calls on the Adaptee. This translation also possibly involves wrapping or modifying parameters and return types.

Adaptee – The interface that defines the methods of the type that will be adapted. This interface allows the specific Adaptee to be dynamically loaded at runtime.

ConcreteAdaptee – An implementation of the Adaptee interface. The class that needs to be adapted so that the Framework can use this class.

For more complex communication, it can be useful to establish an action map to better understand how to manage the communication. An action map is a table showing how the Adapter maps methods and call parameters between the caller and adaptee. Table 3-1 shows the mapping for a single method.

Table 3-1. Example action map

Framework

Adapter Action

Adaptee

method1

None

method2

argument1

None

argument1

argument2

wrapper

argument2

 

create

argument3

The Unified Modeling Language (UML) can effectively represent this; typically, the sequence diagram is a useful tool for action mapping. The Adapter sequence diagram for action mapping is shown in Figure 3.2.

99