Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

Swift adapter design pattern – The.Swift.Dev.

learningcode_x1mckf by learningcode_x1mckf
October 4, 2022
in Swift
0
Swift adapter design pattern – The.Swift.Dev.
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


2018/07/29

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.

Design patterns

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 permits 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 remodel one factor into one other, generally it is referred to as wrapper, as a result of it wraps the article and gives 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 straightforward process to do. You simply have to make a brand new object, “field” the previous one into it and implement the required interface in your new class or struct. In different phrases, a wrapper object will likely 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 (eg. 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).



Easy methods to use the adapter sample in Swift?

You should utilize an adapter if you wish to combine a third-party library in your code, however it’s interface would not match together with your necessities. For instance you possibly can create a wrapper round a whole SDK or backend API endpoints with a view to create a typical denominator. 👽

In my instance, I will 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 present ultimate courses or structs however they lack some performance and also you need to construct a brand new goal interface on prime of them. Typically it is a good selection to implement an wrapper to deal with this messy state of affairs. 🤷‍♂️

That is all in regards to the adapter design pattern. Often it is 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: do not go too laborious on dongles! 😉 #himym




Source link

You might also like

The abstract Vapor service factory design pattern

SwiftNIO tutorial – The echo server

Introducing – Vapor cheatsheet – The.Swift.Dev.

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

The abstract Vapor service factory design pattern

by learningcode_x1mckf
February 1, 2023
0
The abstract Vapor service factory design pattern

I've written a number of articles about manufacturing unit design patterns on my weblog and this time I might like to speak a couple of particular one, which...

Read more

SwiftNIO tutorial – The echo server

by learningcode_x1mckf
January 27, 2023
0
SwiftNIO tutorial – The echo server

Intoducing SwiftNIO In the event you used a excessive degree net framework, corresponding to Vapor, up to now, you would possibly had some interplay with occasion loops...

Read more

Introducing – Vapor cheatsheet – The.Swift.Dev.

by learningcode_x1mckf
January 23, 2023
0
Introducing – Vapor cheatsheet – The.Swift.Dev.

Out there on Gumroad Thanks for supporting my work by buying the cheatsheet. 🙏 Download now A whole Vapor framework reference for novices. greater than...

Read more

iCloud Programming Tutorial for iOS: An Introduction

by learningcode_x1mckf
January 18, 2023
0
iCloud Programming Tutorial for iOS: An Introduction

Editor’s observe: This week, we work with Ziad Tamim once more to provide you an introduction of iCloud programming. You’ll learn to save and retrieve knowledge from iCloud.On...

Read more

Easy multipart file upload for Swift

by learningcode_x1mckf
January 18, 2023
0
Easy multipart file upload for Swift

I imagine that you've got already heard in regards to the well-known multipart-data add method that everybody likes to add recordsdata and submit type knowledge, but when not,...

Read more
Next Post
Type syntax proposed for JavaScript • The Register

Type syntax proposed for JavaScript • The Register

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

What are checked vs. unchecked exceptions in Java?

What are checked vs. unchecked exceptions in Java?

October 9, 2022
Different server for Google API – JavaScript – SitePoint Forums

Discord Rich Presence – JavaScript – SitePoint Forums

February 1, 2023
A shallow crustal earthquake impacts West Java

A shallow crustal earthquake impacts West Java

November 24, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?