Adapter Design Pattern
Adapter Pattern allows incompatible objects to work together. When there are two objects with similar functionality need to collaborate but one of them following a protocol/interface which other object can’t follow, adapter pattern helps these object collaborate. Adapter pattern is also referred as Wrapper because it wraps an incompatible object to make it compatible.
Let’s assume we have a class called TourGuide which is legacy code and can’t be modified. It considers currency as USD and distance in Miles. Recently there is a requirement of writing BritainTourGuide after you know the Brexit happened. BritainTourGuide considers currency as Pound and distance in Kilometers. We have an app in which shows tour guides. There is a screen in the app which shows these guides. Now the problem is how can we show these different tour guides without a lot of conditional code. We don’t have the flexibility to change the TourGuide. We can use something intermediary which takes Pound as input and can convert to USD also takes Kilometer as input and can covert to Miles. In Adapter pattern we call this intermediary as Adapter.
Example:
// TourGuideViewController.swift
import UIKit
class TourGuideViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// This is plain old tour guide.
let tourGuide = TourGuide.init(flightFare:USD(usd:300),
stay:USD(usd:1000), beer: USD(usd:250), byFlight:
Miles(miles:2500), byBike: Miles(miles:1000))
// This is brand new tour guide.
let britainTourGuide = BritainTourGuide.init(flightFare:
Pound(pounds: 150), stay: Pound(pounds: 500), beer:
Pound(pounds: 50), byFlight: Kilometere(kilometeres: 2200),
byBike: Kilometere(kilometeres: 800))
// Using adapter we made britainTourGuide look like an old
tour guide.
let tourGuide1 = TourAdaptor.init(britainTourGuide:
britainTourGuide)
// Now we can show these incompatible made compatible tour
guides.
let tourGuides:[TourProtocol] = [tourGuide, tourGuide1]
self.showTourGuides(tourGuides: tourGuides)
}
func showTourGuides(tourGuides: [TourProtocol]) {
tourGuides.forEach { (tourGuide) in
print(tourGuide.tripCost())
print(tourGuide.tripDistance())
}
}
}// TourGuide.swift
import UIKit
protocol TourProtocol {
func tripCost() -> USD
func tripDistance() -> Miles
}struct Miles {
var miles: Double
}struct USD {
var usd: Double
}class TourGuide: TourProtocol {
var flightFare: USD
var stay: USD
var beer: USD
var byFlight: Miles
var byBike: Miles
init(flightFare: USD, stay: USD, beer: USD, byFlight: Miles,
byBike: Miles) {
self.flightFare = flightFare
self.stay = stay
self.beer = beer
self.byFlight = byFlight
self.byBike = byBike
}
func tripCost() -> USD {
let normalExpense = flightFare.usd + stay.usd + beer.usd
let tips = normalExpense*0.03
let miscellaneous = normalExpense*0.05
return USD.init(usd: normalExpense+tips+miscellaneous)
}
func tripDistance() -> Miles {
return Miles.init(miles: 3000)
}
}import UIKit
class BritainTourGuide: BritainTourProtocol {
var flightFare: Pound
var stay: Pound
var beer: Pound
var byFlight: Kilometere
var byBike: Kilometere
init(flightFare: Pound, stay: Pound, beer: Pound, byFlight:
Kilometere, byBike: Kilometere) {
self.flightFare = flightFare
self.stay = stay
self.beer = beer
self.byFlight = byFlight
self.byBike = byBike
}
func tripCost() -> Pound {
let normalExpense = flightFare.pounds + stay.pounds +
beer.pounds
let tips = normalExpense*0.03
let miscellaneous = normalExpense*0.05
return Pound.init(pounds: normalExpense+tips+miscellaneous)
}
func tripDistance() -> Kilometere {
return Kilometere.init(kilometeres: 3000)
}
}protocol BritainTourProtocol {
func tripCost() -> Pound
func tripDistance() -> Kilometere
}struct Kilometere {
var kilometeres: Double
}struct Pound {
var pounds: Double
}import UIKitclass TourAdaptor: TourProtocol {
private let britainTourGuide: BritainTourGuide
init(britainTourGuide: BritainTourGuide) {
self.britainTourGuide = britainTourGuide
}
func tripCost() -> USD {
let cost = britainTourGuide.tripCost()
let usd = cost.pounds / 2.5
return USD.init(usd: usd)
}
func tripDistance() -> Miles {
let distance = britainTourGuide.tripDistance()
let miles = distance.kilometeres * 0.62
return Miles.init(miles: miles)
}
}
When to choose Adapter Pattern:
Get Saurav Satpathy’s stories in your inbox
Join Medium for free to get updates from this writer.
— Two component shares similar functionality.
— Despite having similar functionality, one of the component has an interface that is incompatible.
— There is no flexibility to change the code.
Advantages:
— Helps achieve reusability and flexibility.
— Client class doesn’t get complicated, by having to use different types of objects and intermediary code.
References: Refactoring, Swiftworld, Jeremy-codes, iamcrypticcoder



