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

Simple factory

  • one factory class
  • switch case objects inside of it
  • encapsulates varying code
  • if list is too big use factory method instead

Factory method

  • multiple (decoupled) factory classes
  • per-instance factory method
  • create a simple protocol for factory

Abstract factory

  • combines simple factory with factory method
  • has a global effect on the whole app

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. 👍

External sources