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 abstract factory design pattern

learningcode_x1mckf by learningcode_x1mckf
October 6, 2022
in Swift
0
Swift abstract factory design pattern
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

The abstract Vapor service factory design pattern

SwiftNIO tutorial – The echo server

Introducing – Vapor cheatsheet – The.Swift.Dev.

Summary manufacturing facility in Swift

The abstract factory sample supplies a technique to encapsulate a bunch of particular person factories which have a typical theme with out specifying their concrete courses.

So abstract factory is there so that you can create households of associated objects. The implementation normally combines easy manufacturing facility & factory method rules. Particular person objects are created by means of manufacturing facility strategies, whereas the entire thing is wrapped in an “summary” easy manufacturing facility. Now test the code! 😅


protocol ServiceFactory 
    func create() -> Service


protocol Service 
    var url: URL  get 



class StagingService: Service 
    var url: URL  return URL(string: "https://dev.localhost/")! 


class StagingServiceFactory: ServiceFactory 
    func create() -> Service 
        return StagingService()
    



class ProductionService: Service 
    var url: URL  return URL(string: "https://dwell.localhost/")! 


class ProductionServiceFactory: ServiceFactory 
    func create() -> Service 
        return ProductionService()
    



class AppServiceFactory: ServiceFactory 

    enum Setting 
        case manufacturing
        case staging
    

    var env: Setting

    init(env: Setting) 
        self.env = env
    

    func create() -> Service 
        change self.env 
        case .manufacturing:
            return ProductionServiceFactory().create()
        case .staging:
            return StagingServiceFactory().create()
        
    


let manufacturing facility = AppServiceFactory(env: .manufacturing)
let service = manufacturing facility.create()
print(service.url)

As you’ll be able to see utilizing an summary manufacturing facility will affect the entire software logic, whereas manufacturing facility strategies have results solely on native elements. Implementation can fluctuate for instance you might additionally create a standalone protocol for the summary manufacturing facility, however on this instance I needed to maintain issues so simple as I might.

Summary factories are sometimes used to realize object independence. For instance you probably have a number of completely different SQL database connectors (PostgreSQL, MySQL, and so forth.) written in Swift with a typical interface, you might simply change between them anytime utilizing this sample. Identical logic may very well be utilized for something with an analogous situation. 🤔



Source link

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
The Javascript Framework That Solves The Annoying Feature-Speed Paradox

The Javascript Framework That Solves The Annoying Feature-Speed Paradox

Leave a Reply Cancel reply

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

Related News

Promises in Swift for beginners

Promises in Swift for beginners

September 29, 2022
Java Full Stack Developer Jobs in 2023

Java Full Stack Developer Jobs in 2023

January 30, 2023
Microsoft Releases Java on VS Code Spring Boot App Dev Extension Pack — ADTmag

Microsoft Releases Java on VS Code Spring Boot App Dev Extension Pack — ADTmag

December 8, 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?