12.4 C
Canberra
Sunday, October 26, 2025

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 facility technique is only a non-static technique

Let’s face it, this sample is only a technique normally backed by easy protocols & courses. Begin with a extremely 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 assume, that hey, this isn’t even near a manufacturing facility technique sample, however anticipate it… let’s make issues slightly bit difficult by making a protocol for the service class and a protocol for returning the URL as nicely. 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 courses and protocols? Please consider me decoupling is an effective factor. Any longer you can 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 can add new sorts in a comparatively painless manner with out altering the unique codebase. The manufacturing facility technique solves one particular drawback of a easy manufacturing facility sample. If the checklist – contained in the switch-case – turns into too lengthy, sustaining new objects can be hell with only one manufacturing facility. Manufacturing facility 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 how you can implement a fundamental occasion processing system in your modular Swift software.


· 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


Learn 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. Learn to use lenses and prisms to control objects utilizing a practical method.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles