The adapter pattern
In the example we have been studying here, we used an adapter pattern to connect our data with our layout in the form of our DataAdapter
. This is a ready-made adapter and although it is clear how it works, it teaches us nothing about the structure of the adapter or how to construct one ourselves.
There are many cases where Android provides built in patterns, which is very useful, but there will often be times when we need an adapter for classes we have created ourselves, and we will now see how this is done and also how to create the associated design pattern, the bridge. It is best to begin by looking at these patterns conceptually.
The purpose of the adapter is perhaps the easiest to understand. A good analogy would be the physical adapters we use when we take electronic devices to other countries where their power outlets work on different voltages and frequencies. The adapter has two faces, one to accept our plug and one to fit the socket. Some adapters are even smart...