Adapter Design Pattern Examples

Learn about the adapter design pattern with some coding examples.

We'll cover the following

Overview

Suppose we have a system (code/client) that requires us to use a legacy triangle. Let’s say that our system can’t directly communicate with the LegacyRectangle class. The LegacyRectangle class expects that a rectangle is specified utilizing the coordinates of the bottom left and top right corners. Meanwhile, our code expects to specify a rectangle with the coordinates of the bottom left corner, the width, and the height.

This means we need an adapter between our system and the LegacyRectangle class. We’ll call this adapter RectangleAdapter. We’ll solve this problem with the inheritance approach, where we’ll inherit the adapter class from the interface (Rectangle class) and implementation (LegacyRectangle).

Code

Let’s look at its code.

Get hands-on with 1400+ tech skills courses.