Sunday, October 15, 2023
HomeiOS DevelopmentSwift adapter design sample - The.Swift.Dev.

Swift adapter design sample – The.Swift.Dev.


Fist of all let me emphasize that, that is the actual world illustration of what we will construct on this little Swift adapter sample tutorial:

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 special object.

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

Adapter design sample implementation

Creating an adapter in Swift is definitely an excellent 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 thing we’re adapting to a particular 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).

Find out how to use the adapter sample in Swift?

You should utilize an adapter if you wish to combine a third-party library in your code, nevertheless it’s interface would not match along with your necessities. For instance you’ll be able to create a wrapper round a complete SDK or backend API endpoints to be able to create a standard denominator. 👽

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

import Basis
import EventKit


protocol Occasion {
    var title: String { get }
    var startDate: String { get }
    var endDate: String { get }
}


class EventAdapter {

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

    non-public var occasion: EKEvent

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


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 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")


let adapter = EventAdapter(occasion: calendarEvent)

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

That is all concerning the adapter design sample. Normally it is very easy to implement it in Swift – or in some other programming language – nevertheless it’s tremendous helpful and generally unavoidable.

Children, bear in mind: do not go too arduous on dongles! 😉 #himym



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments