Adapter pattern in Swift

Putting round pegs in a square hole… or the other way around

Jeremi Kaczmarczyk
Swiftier, faster, better
3 min readOct 16, 2016

--

Structural patterns

Before we dive deep into the Adapter pattern (actually is quite straightforward), let’s say few things about group of the patterns I am going to describe in the next few articles — the Structural patterns. Constructional patterns described methods of creating objects. Structural patterns are all about compositing objects into the greater structures. They are all about relations between interfaces and implementations. Let me take you on a ride through object oriented goodness of the structural patterns.

The Adapter

If you ever had to use 🇪🇺 laptop charger in 🇺🇸 then you probably ecountered adapter (or if you use mini jack headphones with your brand new iPhone 7).

Adapter converts class interface for other, expected by client. Adapter allows to objects to cooperate with objects they could not normally work with due to different interfaces.

By now you should have a clue when do we use adapter. One more thing to mention is that the nature of those objects have to be similar. In real life there are adapters between electrical or camera lens sockets but there is no adapter for camera lens to power socket (please do correct me if I am wrong 🤔). Accidentally it is good rule of thumb for creating adapters in your code.

We have 2 types of the adapters but we can only implement one of them in Swift — the object adapter. For the reference the other one is called class adapter. Please look it up especially if you also programm in C++. It is really useful there.

Simple adapter

It is not a full blown example of adapter but really neat trick to write one. As this one uses the composition, we can cleverly use Swift language features (and you probably did countless times) to create it. Let’s imagine we have hobbit named Pippin as object in our application. For the most part he is just a Hobbit. But he become Guardian of the Citadel at some point. Guardians do not walk, they march, so we have to write adapter for Pippin to march as guardian he became. In Swift it is really intuitive and simple. If GuardianOfTheCitadel is just an interface, all we have to do is to extend Hobbit class with that interface. As simple as that!

Object adapter

Second type of adapter we can write in Swift is so called Object adapter. It is better than above example especially in complex use cases. You can easily extend your class to implement simple interface. But in case where you do not have simple interface or you do not have it at all, Object adapter comes to the rescue.

Using the terms I introduced earlier I will show you how it works, considering worst case scenario that we don’t use too much interfaces in our code (… and we should use them a lot). So back to the code. 🤓

As always, here are key actors in that pattern:

  • Adaptee — it is the object we are adapting to target (our EU laptop charger in US).
  • Adapter — an object which adjusts adaptee to the target. In case of object adapter subclass of target (or EU to US plug adapter).
  • Target — target by now explained itself. It is the object we want to use adaptee with (or US socket).

Allow me to use Hobbit analogy again. Here is the code.

You can clearly see that this adapter is much more flexible than just adding extension as we have some limitations on extenstions in Swift. HobbitGuardian can have its own stored properties and the logic is nicely encapsulated inside such class. It is also more flexible as you can adapt classes (implementation) and if you can just replace any of them for protocol. You can adapt interface-interface, interface-implementation, implementation-implementation. Quite capability for such simple pattern.

Conclusion

You are probably eager to use adapters in your code and to be honest you are using them all the time. Everytime you conform to the protocol you basically create a simple adapter. Another nice but not so clear example is delegation. I know delegation is a design pattern itself widely used in Cocoa but it basically allows (adapt) objects to work with themselves. Imagine UITableView, its Delegate and DataSource. To make table view to work, you have to implement its DataSource methods. By doing that you basically create adapter that allows such class to send its data to table view. As you can see adapters are widely useful and it is worth to know a little bit more about them.

Coming up next: The Decorator Pattern

--

--