Swift manufacturing facility technique design sample

Swift manufacturing facility technique design sample



· 1 min learn


The manufacturing facility technique design sample is a devoted non-static technique for hiding the creation logic of an object. Let’s make it in Swift!

Manufacturing unit technique is only a non-static technique

Let’s face it, this sample is only a technique often backed by easy protocols & lessons. Begin with a very easy instance: think about a category that may create a base URL in your service endpoint. Let’s name it service manufacturing facility. 😅

class ServiceFactory {
    func createProductionUrl() -> URL {
        return URL(string: "https://localhost/")!
    }
}
let manufacturing facility = ServiceFactory()
manufacturing facility.createProductionUrl()

You would possibly suppose, that hey, this isn’t even near a manufacturing facility technique sample, however look forward to it… let’s make issues a bit bit difficult by making a protocol for the service class and a protocol for returning the URL as effectively. Now we will implement our base manufacturing URL protocol as a separate class and return that particular occasion from a manufacturing service manufacturing facility class. Simply test the code you’ll get it:

protocol ServiceFactory {
    func create() -> Service
}

protocol Service {
    var url: URL { get }
}

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

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

let manufacturing facility = ProductionServiceFactory()
let request = manufacturing facility.create()

Why did we separated all of the logic into two lessons and protocols? Please consider me decoupling is an efficient factor. To any extent further you would simply write a mocked service with a dummy URL to mess around with. Clearly that’d want an identical manufacturing facility class.

These mock cases would additionally implement the service protocols so you would add new sorts in a comparatively painless approach with out altering the unique codebase. The manufacturing facility technique solves one particular drawback of a easy manufacturing facility sample. If the listing – contained in the switch-case – turns into too lengthy, sustaining new objects will likely be hell with only one manufacturing facility. Manufacturing unit technique solves this by introducing a number of manufacturing facility objects.

Associated posts


· 5 min learn


On this article I’m going to point out you tips on how to implement a primary occasion processing system in your modular Swift utility.


· 4 min learn


Be taught the iterator design sample by utilizing some customized sequences, conforming to the IteratorProtocol from the Swift normal library.


· 4 min learn


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


· 5 min learn


Newbie’s information about optics in Swift. Discover ways to use lenses and prisms to govern objects utilizing a practical strategy.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.