HomeiOS DevelopmentSwift adapter design sample - The.Swift.Dev.

Swift adapter design sample – The.Swift.Dev.



· 2 min learn


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 actual 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 remodel one factor into one other, generally it’s referred to as wrapper, as a result of it wraps the thing and supplies a brand new interface round it. It’s like a software program dongle for particular interfaces or legacy courses. (Dongle haters: it’s time to go away the previous behind!) 😂

Adapter design sample implementation

Creating an adapter in Swift is definitely a brilliant straightforward activity to do. You simply must 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 item 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 wish to use adaptee with (our USB-C socket).

How one can use the adapter sample in Swift?

You should utilize an adapter if you wish to combine a third-party library in your code, but it surely’s interface doesn’t match together with your necessities. For instance you may create a wrapper round a whole SDK or backend API endpoints as a way 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 {

    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
    }
}

// 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 an alternative of an EKEvent
let adapter = EventAdapter(occasion: calendarEvent)
// adapter.title
// adapter.startDate
// adapter.endDate

One other use case is when you must use a number of current ultimate courses or structs however they lack some performance and also you wish to construct a brand new goal interface on prime of them. Generally it’s a good selection to implement an wrapper to deal with this messy scenario. 🤷‍♂️

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

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

Associated posts


· 2 min learn


Study concerning the initialization strategy of the 2 well-known courses in UIKit. Say hi there to UIViewcontroller, and UIView init patterns.


· 4 min learn


Study the iterator design sample through the use of some customized sequences, conforming to the IteratorProtocol from the Swift normal library.


· 4 min learn


Learn to use lazy properties in Swift to enhance efficiency, keep away from optionals or simply to make the init course of extra clear.


· 1 min learn


Let’s mix manufacturing unit technique with easy manufacturing unit voilá: right here is the summary manufacturing unit design sample written in Swift language!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments