The adapter pattern creates a common class interface for
various sources that use a variety of interface definitions, as we have seen
above. The specific implementations for "Books Express" and "I
Luv Books" inherit from this interface, which acts as a connection between
your application and the third-party application interfaces. That is the sole
purpose of the adapter pattern; using the standardized interface in your
application, it contains all the necessary third-party application code to do
what it needs to do using the standard interface. Let us look at an example of
the adapter classes for the two methods shown above. Here we have the standard
base adapter class for this example.
Listing 2
public abstract class BookAdapter
{
public abstract Book RetrieveByISBN(string isbn);
public abstract Book[] RetrieveBySearch(string searchText, bool anyTerms);
}
This base class defines the interface that the adapter
classes will use. Let us take a look at the BooksExpressAdapter, which is the
adapter used to connect to the "Books Express" repository and pull
back book results. Notice that the adapter uses a Book class, not a BookItem or
DataTable, to represent the book information. This is something unique to the
local application and requires that a transformation of the data take place,
which we will see later.