Spring SALE

Adapter in Swift

Adapter is a structural design pattern, which allows incompatible objects to collaborate.

The Adapter acts as a wrapper between two objects. It catches calls for one object and transforms them to format and interface recognizable by the second object.



Usage examples: The Adapter pattern is pretty common in Swift code. It’s very often used in systems based on some legacy code. In such cases, Adapters make legacy code work with modern classes.

Identification: Adapter is recognizable by a constructor which takes an instance of a different abstract/interface type. When adapter receives a call to any of its methods, it translates parameters to appropriate format and then directs the call to one or several methods of the wrapped object.

The following examples are available on Swift Playgrounds.
Kudos to Alejandro Mohamad for creating the Playground version.

Adapter in Other Languages

Adapter in C# Adapter in C++ Adapter in Go Adapter in Java Adapter in PHP Adapter in Python Adapter in Ruby Adapter in Rust Adapter in TypeScript