This time let's talk about the simple factory design pattern to encapsulate object creation in a really simple way using Swift.


Simple factory implementation using switch-case

The goal of this pattern is to encapsulate something that can often vary. Imagine a color palette for an application. You might have to change the colors according to the latest habit of the designer on a daily basis. I'd be really inconvenient if you had to search & replace every single instance of the color code by hand. So let's make a simple factory in Swift that can return colors based on a given style. 🎩

This can be really useful, especially if it comes to a complicated object initialization process. You can also define a protocol and return various instance types that implement the required interface using a switch case block. 🚦

So, a few things to remember about the simple factory design pattern:

  • it helps loose coupling by separating init & usage logic 🤔
  • it's just a wrapper to encapsulate things that can change often 🤷‍♂️
  • simple factory can be implemented in Swift using an enum and a switch-case
  • use a protocol if you are planning to return different objects (POP 🎉)
  • keep it simple 🏭

This pattern separates the creation from the actual usage and moves the responsibility to a specific role, so if something changes you only have to modify the factory. You can leave all your tests and everything else completely untouched. Powerful and simple! 💪


External sources