Design patterns: Adapter

In this article, as is clear from the title today is about the Adapter design pattern, the name of which can be guessed enables the cooperation of objects that do not match each other 🙂


Description and implementation method

More specifically, in the Adapter pattern, the idea is to make it possible for objects with incompatible interfaces to work together. It is useful, for example, in the use of newer versions of some libraries, eg when we used an old library, but a new library came out and we want to adapt this old one to this new one. Another task of the Adapter is also to pack the old interface into a new one.

In the real world, the adapter is like an adapter to the monitor plug, for example, I recently had to buy a new monitor and the monitor plug did not fit into the computer, so I had to go to the store for an adapter. The adapter has a very similar function in the programming world.



Let’s look at the UML diagram of the adapter pattern

Scheme of Adapter

It shows that the Rectangle class uses the Shape interface and the LegacyRectangle class uses the Rectangle class.



The scheme of operation

Let’s take an example to show what the adapter is about:

I did it on a very simple example, there is a new class and we want it to be compatible with the old class interface without changing the method names, I created for this purpose an adapter class that implements the IClientInterface interface, i.e. we have to create old methods in this class and so we do that and inside SomeMethod1() method create an instance of a new class (let’s not focus on the rules of pure code, it’s important to convey the idea of the adapter) and calls its method.

Notice what happened, we used the new class method, but in the Adapter class, which uses the names of the old class methods, this is more or less what the adapter does.



Phone models

Let’s do an example now that better applies to everyday life, let’s do an adapter for the new version of iphone from 6 to 7:

The iphone6 class looks like this:

And the iphone7 class like this:

We can see that the interfaces of these classes do not match, have different method names, method names in the iphone6 and iphone7 classes, also changed, and we want to use old method names, in which case we need to create an adapter that will be implementing methods with old names and in these methods he will call methods from the new class iphone7, it looks like this:

First, we create an instance of the iphone7 class with an abstract type, then we implement the methods from the iphone6 interface and in them we call methods from the new class.

In the client, in the “Main” method, we call it like this:

First, we call the old version of iphone6, then the Adapter class, which has old methods of the iphone6 class, in both cases works the same.



Change of the electricity system

This example had to be found, in this example we will switch from the Polish electricity system to the American one, this is well illustrated by the picture below.

Let’s move to the code, this example will look very much like the previous one with phones. Let’s start with the PolandElectricalSocket class, ie from the Polish electricity system.

And let’s also see what the class from the American electricity system looks like.

And let’s see the class of the adapter switching the Polish system to the American system.

And finally the customer

You can see that it looks very much like the previous example. If you did an example with phones, I do not know if there is anything to be translated here.



Relations with other design patterns

  • The facade uses a new interface, the adapter uses the old interface.
  • The adapter provides a different interface for the object. Proxy provides the same interface. The decorator provides an extended interface.
  • The bridge is designed so that abstraction and implementation are independent of each other. Thanks to the adapter, unrelated classes are to cooperate with each other.
  • The adapter is used to change the interface of an existing object. The decorator improves another object without changing its interface.



That’s all about Adapter 🙂.

Link to github with the whole code from this article:

This content also you can find on my steemit blog

And on medium:

In the next article, we will talk about the Decorator pattern.

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb:ławomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

If that post was useful for you share it with your friends :)

Post a comment

Be first!

Notify about