Adapter pattern in Swift

Hitendra Solanki
Aug 31 · 3 min read

Updated on 31st Aug 2019, 02:48 AM GMT 5:30+

Thanks to Chris Grooms for the great illustration!

Prerequisites — This tutorials series requires an intermediate level of expertise in object-oriented programming. You should have basic knowledge about class, object, constructor, inheritance, value and reference type. An Intermediates will gain knowledge and experts will sharpen his or her knowledge by reading this series from start to finish.

What is an Adapter?

An adapter is a physical device that allows one hardware or electronic interface to be adapted to another hardware or electronic interface without loss of functionality.

Adapters are used when you have one form of input and you want to access something that cannot take your form of input.

Now It’s time to understand the adapter pattern by answering below simple questions.

  1. What is the Adapter pattern in Object-Oriented programming?
  2. What problem does it solve?
  3. How we can use it in programming?

What is the Adapter pattern in Object-Oriented programming?

In the adapter pattern, we define one class that takes the existing type as an input and give us the required type or required functionality/properties to access.

What problem does it solve?

It helps to convert existing types of data to another form of data.
This is pretty useful when we use code of third party frameworks or library and it accepts restricted types only.

How we can use it in programming?

Let’s understand this with below example.

We have classes likePoint , LineSegment and DrawingPad .

Remember our old school days of Computer Graphics using C language?
That time just to draw a line on the screen, we have to calculate all the points of the line between given two points. To calculate points of line there are multiple basic algorithms available like simple line drawing algorithm, DDA Algorithm, Bresenham’s line algorithm etc.
We will keep our example code clean and easy to understand for the adapter pattern without writing this complex algorithms here.

Adapter design pattern Playground example code

In above example

Line 6, class Point represents a single point.
Line 16, class
LineSegment represents a line segment.
Line 29, class
DrawingPad represents a canvas for drawing. DrawingPad can draw single or multiple points on a canvas.

We can draw a point in this example but can’t draw a line segment directly on the canvas using a drawing pad instance. Here adapter pattern comes in rescue, thanks to adapter pattern.

Adapter design pattern Playground example code

Line45, class LineSegmentToPointAdapter represents an adapter which is the core concept of this article. It takes the existing type LineSegment as an input and gives us an array of points in our case.


  1. An adapter is any class that takes the one object as a parameter in the constructor.
  2. An adapter class has properties/methods that we can use as the required type.
  3. An adapter can also directly act a required type, that depends on implementation.

This is just a simple implementation of the adapter pattern in Object-oriented programming using Swift. But wait, there is something more and advance in this pattern.

The adapter design pattern is more powerful with….

More content coming soon…
Stay tuned, @hitendrahckr is typing

Liked this article?
Give claps and show your support.
It doesn’t cost you anything to clap.

Find my repositories on Github
Read my blogs on Medium
Connect me via LinkedIn
Follow me on Twitter

Stay tuned, @hitendrahckr is typing

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing

Hitendra Solanki

Written by

iOS - Team Lead, “Developer by passion, blogger by public demand”, {Self-taught, Quick Learner} GitHub:

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade