October 2018
Intermediate to advanced
370 pages
9h 15m
English
The Adapter pattern allows us to use a pre-existing interface without modifying the source. Let's imagine that we want to teach a cat how to bark. The following diagram shows how we can implement this:

The preceding diagram contains the Meowable and Barkable interfaces. The Cat class implements the Meowable interface and the Dog class implements the Barkable interface. To teach a cat how to bark, we should pass an instance of the Barkable type to an instance of the Cat class. For this, we can create the Adapter class, which has implemented both interfaces, and invoke the bark method from meow.
The Meowable and Barkable interfaces look ...
Read now
Unlock full access