20.7 C
Canberra
Friday, October 24, 2025

Swift adapter design sample – The.Swift.Dev.


Flip an incompatible object right into a goal interface or class through the use of an actual world instance and the adapter design sample in Swift.

Fist of all let me emphasize that, that is the true world illustration of what we’re going to construct on this little Swift adapter sample tutorial:

Swift adapter design sample – The.Swift.Dev.

Adapter is a structural design sample that enables objects with incompatible interfaces to work collectively. In different phrases, it transforms the interface of an object to adapt it to a distinct object.

So adapter can rework one factor into one other, generally it’s referred to as wrapper, as a result of it wraps the thing and gives a brand new interface round it. It’s like a software program dongle for particular interfaces or legacy lessons. (Dongle haters: it’s time to depart the previous behind!) 😂

Adapter design sample implementation

Creating an adapter in Swift is definitely a brilliant simple activity to do. You simply have to make a brand new object, “field” the outdated one into it and implement the required interface in your new class or struct. In different phrases, a wrapper object might be our adapter to implement the goal interface by wrapping an different adaptee object. So once more:

Adaptee

The article we’re adapting to a selected goal (e.g. old-school USB-A port).

Adapter

An object that wraps the unique one and produces the brand new necessities specified by some goal interface (this does the precise work, aka. the little dongle above).

Goal

It’s the object we need to use adaptee with (our USB-C socket).

The right way to use the adapter sample in Swift?

You need to use an adapter if you wish to combine a third-party library in your code, however it’s interface doesn’t match along with your necessities. For instance you’ll be able to create a wrapper round a complete SDK or backend API endpoints with the intention to create a standard denominator. 👽

In my instance, I’m going to wrap an EKEvent object with an adapter class to implement a model new protocol. 📆

import Basis
import EventKit

// our goal protocol
protocol Occasion {
    var title: String { get }
    var startDate: String { get }
    var endDate: String { get }
}

// adapter (wrapper class)
class EventAdapter {

    personal lazy var dateFormatter: DateFormatter = {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy. MM. dd. HH:mm"
        return dateFormatter
    }()

    personal var occasion: EKEvent

    init(occasion: EKEvent) {
        self.occasion = occasion
    }
}

// precise adapter implementation
extension EventAdapter: Occasion {

    var title: String {
        return self.occasion.title
    }
    var startDate: String {
        return self.dateFormatter.string(from: occasion.startDate)
    }
    var endDate: String {
        return self.dateFormatter.string(from: occasion.endDate)
    }
}

// let's create an EKEvent adaptee occasion
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm"

let calendarEvent = EKEvent(eventStore: EKEventStore())
calendarEvent.title = "Adapter tutorial deadline"
calendarEvent.startDate = dateFormatter.date(from: "07/30/2018 10:00")
calendarEvent.endDate = dateFormatter.date(from: "07/30/2018 11:00")

// now we will use the adapter class as an Occasion protocol, as a substitute of an EKEvent
let adapter = EventAdapter(occasion: calendarEvent)
// adapter.title
// adapter.startDate
// adapter.endDate

One other use case is when you need to use a number of present last lessons or structs however they lack some performance and also you need to construct a brand new goal interface on prime of them. Generally it’s a sensible choice to implement an wrapper to deal with this messy state of affairs. 🤷‍♂️

That’s all in regards to the adapter design sample. Normally it’s very easy to implement it in Swift – or in every other programming language – however it’s tremendous helpful and generally unavoidable.

Children, keep in mind: don’t go too laborious on dongles! 😉 #himym

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles