Join us

Design Patterns in Python: Adapter Pattern

It is a structural design pattern and works as the electronic equivalent of its name. In this respect, it has a successful naming, and its purpose is understandable (it is also called wrapper). If you remember, structural design patterns were concerned with different classes working together. The adapter design pattern also ensures that incompatible interfaces are interoperable. It converts the interface of one class to the interface that the other class expects.

Implementation of Adapter Design Pattern in Python

It is a structural design pattern and works as the electronic equivalent of its name. In this respect, it has a successful naming, and its purpose is understandable (it is also called wrapper). If you remember, structural design patterns were concerned with different classes working together. The adapter design pattern also ensures that incompatible interfaces are interoperable. It converts the interface of one class to the interface that the other class expects.

Photo by Call Me Fred on Unsplash (source)

Previously on Prison Break:

https://medium.com/@okanyenigun/design-patterns-in-python-flyweight-pattern-ec3d321a86af

Let’s start with the classic example. We work as a salesperson in an international company. Our job requires us to travel frequently. Different types of sockets are used in many countries. Therefore, we may have difficulties plugging our electronic devices into sockets in the countries we go to. Here’s what we can do about it:

Plug types: Source: Pegasus (source)

The first option might be to take all the types with us. The structure here is the if-else conditional statements in the system. We choose the necessary one according to the country of destination. The problem here is that this structure is against the Open-Closed Principle (OCP). A new condition will be required for each new country.

As another solution, we create subclasses for each type of socket objects, and override its methods. In this case, there is an explosion in the number of subclasses. In such a solution actually, the adapter is embedded inside the object. The object had to undertake an adapter mission apart from its own function. This is against the Single Responsibility Principle (SRP). This is still better than the first solution but not an ideal solution.

According to SRP, we have to define the adapter task elsewhere. This is a separate responsibility. We will have a separate class that will solve the compatibility problem. This class will make an incompatible object interoperable with another interface with which it is incompatible.

Thanks to the Adapter Design Pattern, the incompatible object gets a new interface, and the adapter becomes the wrapper of the incompatible object. It is also possible to design the adapter to adapt to all interfaces. But in this case, the adapter object itself will become very complex. So, in practice, it would be better if every adapter solves a compatibility problem (two-way).

Implementation

We have 4 components in this pattern structure.

  • Target: Domain-specific interface requested by the Client.
  • Client: Applies business logic, and collaborates with the target.
  • Adaptee: The interface which is incompatible with the target. Needs adaptor.
  • Adapter: The compatibility object between the Adaptee and the Target.

Source: geeksforgeeks (source)

UML Class Diagram of the example. Image by the author.

We have applied the SRP and OCP principles by using the Adapter Design Pattern. Our socket and plug objects have a simpler structure as they are only busy with their work. Likewise, the adapter does its job. We made different interfaces work together.

Thanks for reading. If you have any questions or comments, please feel free to write me!


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

User Popularity
41

Influence

3k

Total Hits

3

Posts