Join us
@okanyenigun ă» Jun 28,2022 ă» 2 min read ă» 894 views
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.
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.
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:
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.
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!
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.