Design Patterns in Swift

Introduction

Jeremi Kaczmarczyk
Swiftier, faster, better

--

As a Swift and iOS engineers we have plenty tools and frameworks at disposal to help us deliver mobile applications. If you are working on small or even medium sized project you can just code your way through to the final release. Working with storyboards reduce amount of boilerplate code by a ton and even though I try to avoid them, you can see how much Apple wants us using them at every WWDC. MVC architecture is heavily promoted by Apple and various development courses as default on iOS. But as project grows the code written that way tend to go wild. You probably encountered the Massive View Controller somewhere during your career. As I see, write, review and test more and more code with every day I am getting more interested in the software architecture…

Why bother?

I used to think that the software architecture is for the big companies which are maintaining millions lines of difficult and important applications. But when at my current company we started to implement some of popular design patterns into our projects things got more structured, easier to collaborate and argue about. When you see a Factory class in a project you know something is going to be built 🏭.

Never stop learning

To start improving myself in that area, to become fluent and conscious user of patterns I borrowed myself classic book titled “Design Patterns” by Erich Gamma, Richard Helm, Ralph Johnson and John Vissides 📖. Some call them The Big Four. You can’t find many more classic IT books than the “Design Patterns”. After an introduction and a case study about design of the text editor application I thought WOW! That is really great. I immediately saw how possessing that knowledge will help me become a better developer. Those patterns are so well thought and tested over the years that not knowing them is usually reinventing the wheel. It is especially true on commercial project with tight time constraints. ⏲

So what?

Well, the problem is that the code in the “Design Patterns” is written mainly in C++ and Smalltalk which reads great but it can not be directly translated to Swift code (especially a modern, protocol and value type oriented code). Thats why I decided to make a little catalogue of classic design patterns for myself. One that would contain 100% Swift code and iOS / macOS use cases so it would be really easy to choose and implement any of them in projects I am working on. But as I took a lot of knowledge from the great Swift community I decided I should describe my journey on blog for others to enjoy.

Structure 🏗

I am going to walk through every pattern from the “Design Patterns” book, describe it and translate to Swift. I am going to release new articles in weekly or probably sometimes bi-weekly intervals. The order of described patterns will be just as in the book:

  1. Constructional Patterns
  2. Structural Patterns
  3. Operational Patterns

Note 1: It is my first ever blog post. 😱

Note 2: This blog would be more than journey with the “Design Patterns” but I hope it is a great way to start it out. 🎉

Update: Constructional Patterns

I have finished posts on all classic constructional patterns, below you can find links for quick access 😄

  1. Builder
  2. Abstract Factory
  3. Factory Method
  4. Prototype
  5. Singleton

In progress: Structural Patterns

  1. Adapter
  2. Decorator

--

--