Adapter pattern

Sometimes existing classes already provide functionalities required by client, but its interface is not what the client expects. So that we need a bridge to convert existing interface to what the client expects. And the bridge is called an Adapter. In real world, an adapter is very popular such as adapters for supplies, adapters for memory cards, so on. In software programming, the features of adapter are provided by Adapter pattern. We are going into its details in this article.

Adapter pattern helps us with two main jobs:

  • Convert the interface of a class into another interface what client expects.
  • Adapter lets classes work together, that couldn’t be able because of incompatible interfaces.

Basically, Adapter pattern has four major components:

  • Client collaborates with objects conforming to Target objects.
  • Target defines interface that client needs.
  • Adapter converts Adaptee interface to Target interface.
  • Adaptee defines existing interface.

For more details, we will go into two forms of Adapter pattern: Object Adapter pattern and Class Adapter pattern.

1. Object Adapter pattern

Adapter class diagram 1

Object adapter pattern adapts the Adaptee interface to the Target interface by using object composition. This means an adapter object contains one or more references of adaptee objects, and it also implements Target interface. Thus, when client object calls target’s functions, the adapter object invokes appropriate ones of adaptee instances whose references it contains.

2. Class Adapter pattern

Object Adapter pattern class diagram

Class Adapter pattern, in the difference way, uses inheritance instead of object composition to implement Adapter pattern. In this way, a class adapter extends adaptee class, and also implements Target interface. When client object calls Target’s functions, adapter object will call the override functions from adaptee.

3. Comparison of Class Adapter pattern and Object Adapter pattern

Class Adapter pattern Object Adapter pattern
Use inheritance mechanism Use object composition
Can used to adapt only the interface of adaptee, cannot adapt its subclasses. Can used to adapt adaptee interface and all its subclasses.
The client has some knowledge about adaptee's interface, such as  its public interface. The client knows adapter's interface  only.
In Java programming, specifically Android, we should use Class adapter when target interface is in form of Java interface, not abstract class or concrete class. It's because of single inheritance in Java. In Java programming, suitable when expected interface has abstract class form. Or even interface form.

4. Example

In Android programming, there is an very common example of adapter pattern – push a list of custom object to ListView, and it is an example for object adapter form. However, in this article I want to write an class adapter example in Java language. You can download full source code here. And bellow is class diagram of sample, it is quite simple so you can understand easily.Rect class adapter diagram

Leave a Reply

Your email address will not be published. Required fields are marked *