Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Working with @Generable and @Guide in Foundation Models

    July 18, 2025

    Navigating the labyrinth of forks

    July 18, 2025

    OpenAI unveils ‘ChatGPT agent’ that gives ChatGPT its own computer to autonomously use your email and web apps, download and create files for you

    July 18, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»Comparing factory design patterns – The.Swift.Dev.
    iOS Development

    Comparing factory design patterns – The.Swift.Dev.

    big tee tech hubBy big tee tech hubJuly 2, 2025003 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    Comparing factory design patterns – The.Swift.Dev.
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    6/5/18 2:20 PM
    · 1 min read


    Learn what’s the difference between static factory, simple factory, factory method and abstract factory using the Swift language.

    I thought that I’d be nice to have a summarized comparison between all the factory patterns, so here it is everything that you should know about them. Constructing them is relatively straightforward, in this example I’m going to use some UIColor magic written in the Swift programming language to show you the basics. 🧙‍♂️

    Static factory

    • no separate factory class
    • named static method to initialize objects
    • can have cache & can return subtypes
    extension UIColor {
        static var primary: UIColor { return .black }
        static var secondary: UIColor { return .white }
    }
    
    let primary = UIColor.primary
    let secondary = UIColor.secondary
    

    Simple factory

    • one factory class
    • switch case objects inside of it
    • encapsulates varying code
    • if list is too big use factory method instead
    class ColorFactory {
        enum Style {
            case primary
            case secondary
        }
    
        func create(_ style: Style) {
            switch style
            case .primary:
                return .black
            case .secondary:
                return .white
        }
    }
    let factory = ColorFactory()
    let primary = factory.create(.primary)
    let secondary = factory.create(.secondary)
    

    Factory method

    • multiple (decoupled) factory classes
    • per-instance factory method
    • create a simple protocol for factory
    protocol ColorFactory {
        func create() -> UIColor
    }
    
    class PrimaryColorFactory: ColorFactory {
        func create() -> UIColor {
            return .black
        }
    }
    
    class SecondaryColorFactory: ColorFactory {
        func create() -> UIColor {
            return .white
        }
    }
    
    let primaryColorFactory = PrimaryColorFactory()
    let secondaryColorFactory = SecondaryColorFactory()
    let primary = primaryColorFactory.create()
    let secondary = secondaryColorFactory.create()
    

    Abstract factory

    • combines simple factory with factory method
    • has a global effect on the whole app
    // exact same factory method pattern from above
    protocol ColorFactory {
        func create() -> UIColor
    }
    
    class PrimaryColorFactory: ColorFactory {
        func create() -> UIColor {
            return .black
        }
    }
    
    class SecondaryColorFactory: ColorFactory {
        func create() -> UIColor {
            return .white
        }
    }
    
    // simple factory pattern from above using the factory methods
    class AppColorFactory: ColorFactory {
    
        enum Theme {
            case dark
            case light
        }
    
        func create(_ theme: Theme) -> UIColor {
            switch theme {
            case .dark:
                return PrimaryColorFactory().create()
            case .light:
                return SecondaryColorFactory().create()
            }
        }
    }
    
    let factory = AppColorFactory()
    let primaryColor = factory.create(.dark)
    let secondaryColor = factory.create(.light)
    

    So these are all the factory patterns using practical real world examples written in Swift. I hope my series of articles will help you to gain a better understanding. 👍


    Comparing factory design patterns – The.Swift.Dev.

    Share this article

    Thank you. 🙏

    Related posts

    Comparing factory design patterns – The.Swift.Dev.

    6/5/18 2:20 PM
    · 1 min read


    Learn what’s the difference between static factory, simple factory, factory method and abstract factory using the Swift language.



    Source link

    Comparing Design factory Patterns The.Swift.Dev
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    Working with @Generable and @Guide in Foundation Models

    July 18, 2025

    Deep dive into Swift frameworks

    July 17, 2025

    How to parse JSON in Swift using Codable protocol?

    July 16, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Working with @Generable and @Guide in Foundation Models

    July 18, 2025

    Navigating the labyrinth of forks

    July 18, 2025

    OpenAI unveils ‘ChatGPT agent’ that gives ChatGPT its own computer to autonomously use your email and web apps, download and create files for you

    July 18, 2025

    Big milestone for the future of quantum computing.

    July 18, 2025
    Advertisement
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Working with @Generable and @Guide in Foundation Models

    July 18, 2025

    Navigating the labyrinth of forks

    July 18, 2025

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2025 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.