Other Tutorials

Adapter Design Pattern In Java


The Adapter design pattern is a structural design pattern that helps us to connect to the legacy or third-party code that exposes a similar functionality through a different interface.

A real-world analogy for an adapter is the one we use to connect our USB cable to an ethernet port.

While designing an object-oriented application, we might feel the need for an adapter when say our client expects an object of a specific type and we have a third-party API offering the same functionality but through an incompatible interface. 

Adapter Pattern

It’s also popularly known as a wrapper as it wraps an existing code with a new interface making it compatible for the client.


Let’s be aware of the terms we use when talking about an adapter pattern:

  • Client: the class that wants to use the third-party library or the external system
  • Adaptee: a class in the third-party library or the external system that we want to use
  • Target interface: the desired interface that the client will use
  • Adapter: this class sits between the client and the adaptee and implements the target interface

Using the Adapter Pattern:

Let’s say we have a ShopInventory which maintains a list of products. Later on, we took over another store inventory which sells groceries. We now want to add those items to our ShopInventory. The problem we have here is that although the GroceryItem is just a type of product but is¬†unrelated to the Product interface.

To solve this problem, we’ll use the adapter pattern. We’ll create a GroceryItemAdapter which will implement the Product interface:Adapter Pattern Example

With the help of an adapter, we’ll now be able to treat the GroceryItem as a Product without changing anything in the third-party code(GroceryItem).

Java Implementation:

Let’s first start by defining a Product and a ShopInventory class:

The third-party store that we have just taken over holds GroceryItem‘s:

Since our ShopInventory only holds items of type Product, let’s create an adapter for the newly introduced GroceryItem:

With that, we can now add both our regular products and the grocery items to our ShopInventory:


The adapter pattern helps us to connect two incompatible interfaces exposing the same business functionality.

With an adapter pattern, we convert an existing interface to another interface that the client code expects.

Be the First to comment.

Leave a Comment

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