backgroundLayer 1Navigate back to the homepage

Adapter pattern

Patryk Jeziorowski
December 25th, 2019 · 2 min read

Introduction

In this post, we’ll introduce a popular design pattern - Adapter. Adapters are common in everyday life, so you should have an idea what it comes down to. If not, take a quick look at the image below:

Adapter pattern example

So, as you can see in the picture, the purpose of the adapter is to take one interface and make it compatible with another. In programming, its purpose is the same - you’ll want to use this pattern when you have some type/class and you want to make it compatible with another.

How to do the trick?

In Java, this is done by creating an Adapter class, which extends the type we target (our desired interface) and stores a reference to Adaptee (object that needs to be adapted).

Elements of the pattern

  • Target - our desired type
  • Adaptee - type that needs to be adapted
  • Adapter - class that makes Adaptee compatible with Target

Relations between components

The client of our code (i.e. any place where our classes are used) use Adapters methods, which are compatible with Targets interface, but the work underneath is delegated to Adaptee object.

Code example

If you have any difficulties understanding the purpose of this pattern, looking at some code should definitely help.

First, let’s declare the interface that we target (the one we want to adapt to)

1interface Target {
2 String getFirstName();
3 String getLastName();
4}

Now, we add a class that will be adapted (it is now incompatible with Target interface)

1class Adaptee {
2 String getFullName() {
3 return "Patryk Jeziorowski";
4 }
5}

As you can see, interfaces of Target and Adaptee differs. Target has two methods - the first method returns a first name and the second returns a last name. In Adaptee, there’s only one method that returns first name + last name at once. We can adjust Adaptee to fit Target interface with an Adapter, though!

1public class Adapter implements Target {
2
3 private final Adaptee adaptee = new Adaptee();
4
5 @Override
6 public String getFirstName() {
7 return adaptee.getFullName().split(" ")[0]; // just a dumb example impl
8 }
9
10 @Override
11 public String getLastName() {
12 return adaptee.getFullName().split(" ")[1]; // just a dumb example impl
13 }
14
15}

The Adapter is compatible with Target by simply implementing it, and under the hood, it delegates the work to the Adaptee object. This allows us to reuse Adaptee implementation anywhere we need object of Target type.

At the end, let’s take a look at our code in action:

1public class ClientCode {
2 public static void main(String[] args) {
3 final Target target = new Adapter(); // We need Target type, we can use Adapter!
4 System.out.println(target.getFirstName()); // Work delegated to Adaptee under the hood
5 System.out.println(target.getLastName()); // Work delegated to Adaptee under the hood
6 // output:
7 // Patryk
8 // Jeziorowski
9 }
10}

Anywhere the Target type is needed, we can use Adapter instead, which delegates the hard work to Adaptee.

1final Target target = new Adapter();
1// Adapter impl
2 @Override
3 public String getFirstName() {
4 // use adaptee
5 return adaptee.getFullName().split(" ")[0];
6 }

Conclusion

In this post, we covered all the theory basics of the Adapter pattern. We also discussed a simple code example that makes a use of it.

Keep it all in mind anytime you need to adjust one of your classes to be compatible with another type. Thanks for reading!

Join our email list and get notified about new content

Be the first to receive our latest content with the ability to opt-out at anytime. We promise to not spam your inbox or share your email with any third parties.

More articles from talkoverflow

Learn Golang - Basics

Introduction Go is a modern programming language that is very simple and quick to learn. It makes full use of the power of today’s…

December 23rd, 2019 · 5 min read

What are design patterns?

High-level overview OOP (object-oriented programming) gurus once concluded that it's pointless to reinvent the wheel every time you…

December 21st, 2019 · 1 min read
© 2019–2020 talkoverflow
Link to $https://facebook.com/talkoverflowsoftware/Link to $https://twitter.com/pjeziorowskiLink to $https://github.com/pjeziorowskiLink to $https://www.linkedin.com/in/patryk-jeziorowski-81587414a/