Part 3 of 6 · 1 min read
Two forms
Composition or inheritance.
GoF describes two forms. An object adapter holds an instance of the adaptee and forwards to it, as above. A class adapter inherits from the adaptee and implements the target, which needs multiple inheritance or a mix of a class and an interface.
GoF compares them. A class adapter commits to one concrete adaptee class, so it cannot adapt a class together with all its subclasses. It lets the adapter override some of the adaptee's behaviour, and it uses only one object. An object adapter can serve the adaptee and all its subclasses with one adapter, and it can add behaviour to all of them. It is harder to override the adaptee's behaviour.
In Java a class has only one superclass, so the object adapter is the usual form. It also matches the advice of Level 1, module 4 to prefer composition.
GoF also asks how much the adapter should do. The range runs from changing names of operations, to converting data, to supporting a different set of operations altogether. The more different the two interfaces are, the more work the adapter does. Keep adapters thin. If one grows rules of its own, move those into your own domain classes.
Check yourself: 1. Which form is usual in Java, and why? 2. What is a limit of a class adapter?