2.1 C
Canberra
Monday, October 27, 2025

Swift summary manufacturing facility design sample



· 1 min learn


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

Summary manufacturing facility in Swift

The summary manufacturing facility sample offers a technique to encapsulate a gaggle of particular person factories which have a standard theme with out specifying their concrete courses.

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

// service protocols
protocol ServiceFactory {
    func create() -> Service
}

protocol Service {
    var url: URL { get }
}

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

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

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

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

// summary manufacturing facility
class AppServiceFactory: ServiceFactory {

    enum Atmosphere {
        case manufacturing
        case staging
    }

    var env: Atmosphere

    init(env: Atmosphere) {
        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 may see utilizing an summary manufacturing facility will affect the entire utility logic, whereas manufacturing facility strategies have results solely on native components. Implementation can differ for instance you would additionally create a standalone protocol for the summary manufacturing facility, however on this instance I wished to maintain issues so simple as I may.

Summary factories are sometimes used to realize object independence. For instance in case you have a number of completely different SQL database connectors (PostgreSQL, MySQL, and so on.) written in Swift with a standard interface, you would simply change between them anytime utilizing this sample. Similar logic may very well be utilized for something with an identical state of affairs. 🤔

Associated posts


· 5 min learn


On this article I’m going to indicate you learn how to implement a primary occasion processing system to your modular Swift utility.


· 4 min learn


Be taught the iterator design sample through the use of some customized sequences, conforming to the IteratorProtocol from the Swift customary 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 strategy.

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