top of page

Mastering Swift's Development Blog

Follow Us On Twitter
  • Writer's pictureJon Hoffman

Adapter Pattern: Protocol Oriented Design Pattern

Updated: Sep 1, 2022


The adapter pattern is a structural design pattern that enables the interface of an existing type to be used as a different interface. The adapter will bridge the gap between the two interfaces, enabling the type to be used with either interface, without changing the type themselves.


Problem We Are Trying To Solve

We have several types that present a certain interface however we really need the functionality that they provide with a different interface. We may be unable to change the types themselves because it would break existing code or we may not have access to the source code.


Our Solution

Our solution to this problem will be to extend the types to implement the interface that we wish to use. This will leave the present types untouched and will add the additional interfaces that we need.


The Example

In this example, in our application we are modeling vehicles. The application is being used in the United States therefore we are presenting the speed in miles per hour (MPH). After many revisions of our very successful application, we were just told that we have a new client in Europe that wants to use our application, therefore we will need to present the speed, for this one client and hopefully many more in Europe, in kilometer per hour (KPH).




As usual in our design pattern tutorials, we are going to be showing the minimal amount of code necessary to demonstrate this pattern because we want the focus of this post to be on how the adapter design pattern works and not on implementing the example.


In our application, we already had a Vehicle protocol and several types that adopted it already defined. The following code shows this.

protocol Vehicle {
    var speedMPH: Double { get set }
}


struct DodgeCharger: Vehicle {
    var speedMPH: Double = 0.0
}

struct DodgeChallenger: Vehicle {
    var speedMPH: Double = 0.0
}

We may not have access to the code to change the Vehicle protocol or we may not want to change it, therefore we will define a new protocol for vehicles that will be used in the European version of our application. We will call this protocol VehicleEurope.

protocol VehicleEurope {
    var speedKPH: Double { get set }
}

We can now extend our types, using Swift extension, enabling them to adopt this new protocol. The following code shows how we could do this.

extension DodgeChallenger: VehicleEurope {
    var speedKPH: Double {
        get {
            return speedMPH * 1.60934
        }
        set {
            speedMPH = newValue * 0.621371
        }
    }
}

The Dodge Challenger type can now be used in the European version of our application. The adapter pattern is very useful when we need our types to adopt different functionality and/or interfaces however we do not wish to change or we are not able to change the type themselves.




masteringSwift.jpeg

Mastering Swift 5.3

The sixth edition of this bestselling book, updated to cover through version 5.3 of the Swift programming language

Amazon

Packt

pop.jpeg

Protocol Oriented Programming

Embrace the Protocol-Oriented design paradigm, for better code maintainability and increased performance, with Swift.

Amazon

Packt

bottom of page