Adapter | Cheat Sheet

Structural Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
4 min readJun 12, 2021

--

An Adapter interacts as inter-mediator between Adaptee and Target object. It enables a collaboration amongst classes that normally would not be able to do so due to incompatibility. There are differences between Class Adapter and Object Adapter.

The Adapter pattern is often applied after the system design is complete to allow unrelated classes to work together (resolving incompatibilities). The Bridge pattern, on the other hand, is incorporated into the system design from the very beginning.

Real-life examples

  • Inter-mediator that takes output from one thing, converts it, and puts it into another one
  • Power plug and socket standards for devices for different countries
  • USB-C to USB 3.0 plug

Meaning

  • Adaptation of the interface of a class to another expected interface
  • Enables collaboration of classes that would not be able to do so due to incompatibility

Applicability (suitable when …)

  • An interface, which does not meet the current requirements, is supposed to be used
  • Reusable class is created that is incompatible with existing classes or interfaces
  • Interfaces of the base classes are to be adapted
  • If multiple inheritance is possible, use the class adapter — otherwise use the object adapter

Assets and Drawbacks

  • [Class Adapter] does not work if classes are to be adapted together with their subclasses, since the adapted object has been adapted to the target interface by assigning the adaptee class
  • [Class Adapter] uses only one object
  • [Object Adapter] allows individual adapters to work with multiple adapted objects
  • [Object Adapter] complicates overriding the behavior of the adapter object
  • Different adaptation efforts when using an adapter (from low to complex)
  • Pluggable Adapters — direct integration in reusable classes of the same interface
  • Two-way Adapter, if different clients need the same transparency in both directions

Example

For this example I was inspired by Geeks For Geeks, which remains in my mind since I’ve read it for the first time.

Let’s design a toy that imitates a real animal.

First of all, you need two kinds of interfaces: one for the Target and one for the Adaptee.

Adaptee
Target

The Adapter model interacts as an inter-mediator and connects the Target and the Adaptee.

The client can use the adapter, so a toy imitates a cat in an easy way now.

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Structural Patterns are indispensable when objects are combined to create new functionalities. Comprehensive structures are built by using composition of classes and objects. This provides flexibility due to variable composition structure depending on the runtime. One thus achieves an increase in efficiency and consistency.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links