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

Figure 3.2. Sequence diagram for action mapping

Benefits and Drawbacks

The Adapter offers greatly improved reuse, allowing two or more objects to interact that would otherwise be incompatible. However, some planning and forethought are required in order to develop a framework flexible enough to be conveniently adaptable. This problem has two aspects: functional call structure and parameter translation.

If there is a functional mismatch between the call framework and the Adaptee, the Adapter needs to manage the call requirements of the Adaptee, invoking any required setup methods before the framework’s call can be satisfied.

Another challenge for the Adapter is the transfer of parameters, since passed parameters are not always compatible between the framework and the Adaptee. In these cases, the Adapter usually either creates appropriate objects when there is no direct equivalent between the two environments, or wraps an object to make it usable by the Adaptee.

The most generic of environments for the Adapter are typically built around the Command pattern, using some form of messaging or introspection/reflection. In its most generic form, the Command pattern might eliminate the need for an Adapter. (See “ Command ” on page 51.)

Pattern Variants

Adapters are, by their very nature, dynamic and it’s rare to see two that are exactly alike. Nevertheless, there are some common variations. Three of these common variations are listed here:

Multi-Adaptee Adapters – Depending on the system design, it can be advantageous to make an Adapter part of the calling framework. Such an Adapter often acts as an intermediary between the system and multiple Adaptees.

Non-Interface-based Adapters – Use of the interface in the Java programming language makes it possible to develop even more flexible Adapters. But it may not always be possible to use interfaces. For instance, it’s not possible when you're given complete components that do not implement any interface. In those situations you will see the Adapter pattern used without using interfaces. It goes without saying that these implementations are less flexible.

An interface layer between the caller and Adapter, and another between the Adapter and Adaptee – An interface layer between caller and Adapter allows new Adapters to be more easily added to the system during runtime. An interface between Adapter and Adaptee allows the Adaptees to be dynamically loaded during runtime. In combination, these interface layers make it possible to develop a truly pluggable Adapter design, where the Adaptees can be changed as needed in a running system.

Related Patterns

Related patterns include the following:

100