1.2.2. Adapter Structure

From the book:

In Figure 2, the Adapter implements the Target interface and delegates to the class that we want to adapt, in our case Adaptee. Since our adaptee is an object inside our adapter, we call this an “object adapter”. The object adapter uses composition to adapt the Adaptee.

Figure 2. Structure of the object-adapter design pattern.

An example of an object adapter in Java is the InputStreamReader, which adapts an InputStream into a Reader.

A lesser used form of adapter occurs when our Adapter extends the Adaptee and implements the Target interface. Since the adaptee is a superclass of our adapter, we call this a “class adapter”, as shown in Figure 3. The class adapter uses inheritance to adapt the Adaptee.

Figure 3. Structure of the class-adapter design pattern.

An advantage of the class adapter is that it makes it easier to modify parts of an adaptee. Unfortunately, the trade-off is that it becomes harder to adapt a hierarchy of objects. In a later chapter, we shall see how to get the extensibility of the class adapter and the flexibility of the object adapter using a dynamic proxy.

Complete and Continue  
Discussion

0 comments