Purpose
The Adapter
(structural pattern) allows two classes with incompatible interfaces to work by transforming the interface of one class into an intelligible form for the other. Adapter
otherwise called Wrapper
is a kind of adapter between two entities. There are two variants of the classic form, Class Adapter
and Object Adapter
. The choice of the Adapter
type can be arbitrary or dictated by the constraints depending on the conditions. In the situation when the adaptable class can not be extended, the Object Adapter
is used, whereas if the adapted class is abstract Class Adapter
is used. Due to the danger of using the Class Adapter
, in most situations the Object Adapter
is used. In addition it is referred to as the Bidirectional Adapter
, in which the translation takes place in both directions, i.e. the client is also an adapted class. Such a mechanism can be used, for example to convert types.
Limitations
When creating the Adapter
should be kept in mind that with the increase of incompatibility, performance decreases, which may be a problem in some applications. Object Adapter
can not overload adaptive class methods, while Class Adapter
does not allow adaptation of subclasses of the adapted class. Class Adapter
strictly binds the client with the adapted class, which in the case of system growth or change of requirements, reduces the code reusability.
Usage
It is mainly used where the use of an existing class to communicate with another class is impossible due to the interface’s incompatibility. In addition, Adapter
is used in situations where the created class will work with classes with undefined interfaces.
Implementation
Class Adapter
inherits from the adaptive Adaptee
class and implements the client Client
class. The Object Adapter
implements the client Client
class and contains an instance of the adapted Adaptee
class.
The Client
and Adaptee
class are incompatible with the following listing.
To enable communication, enter Adapter
between them. The following listing presents implementations of the template in two variants.
The adapters prepared in this way allow calling methods of the adapted Adaptee
class from the Client
class, which is presented in the following listing.
Example
Messenger
application allows to send text messages and files between users using the internal library of the network protocol. In addition, the application allows you to change the status. The following listing illustrates implementations of the Messenger
application with its internal MessageProtocol
protocol.
At some point in the project life cycle, replacing the MessageProtocol
network protocol with a more efficient ExternalMessageProtocol
external library is needed. In addition, each operation is to send data for analytics. Due to the size of the project, it is not possible to change the type of the protocol instance and method calls in all instances of the Messenger
class object. The solution to this problem is to use the Adapter
, implementation of which is shown below.
To use the new solution, simply pass the Adapter
reference to the application as follows.
Libraries
Adapter
is a standard feature of the Android
library. Its purpose is to provide data to the view of the AdaperView
collection, as well as to create views View
for each element from the data set.