iOS Design Patterns & Flavors with Unit Tests by Example

There’re many articles about design patterns that provide a good amount of information on either a comparison between different design patterns or on how to get started, and some are really good and can help you get a grasp of the theory and get you started. But some of them fall short in providing examples (real-life examples) for you to play around with them. If you are like me (a very visual and by example kind of learner) these articles may not be enough for you to feel confident to use on your projects.

Another is that there are many different flavors of each design pattern and this makes it even harder for you to select the best design pattern for your project. These articles may help you up get started but then you might find your self searching for other examples that not necessary follow the same flavor that you already started your project with.

With this article, I want to provide a centralized source of examples of different design patterns and flavors. Initially I added three different design patterns and on the follow-up post I’ll add two flavors (one for MVVM and one for VIPER.

Approach

I wrote the same app using three different design patterns (MVVM, MVC and VIPER).

The app uses URLSession to get jobs using GitHub Jobs API and either uses the user location or a location input by the user. The request uses the location and the input text to get available jobs for that location.

App Breakdown

Services:

  • API Client
  • Location Services
  • Image Handler (Image loader and caching)

Views:

  • Job Search/List
  • Job Detail
  • Job Description Detail

Content

The different design patterns approach is separated into different branches:

Model-View-Controller

Model-View-ViewModel Dependency Injection

Model-View-ViewModel Environment Singleton

VIPER-Presenter-Router

All approaches use an environment singleton. Only theMVVM has an alternate approach that uses dependency injection (DI) with protocols. They all have unit tests, except forMVC.

What’s Next?

As mentioned above, the next article will have two new flavors (Semi-Reactive MVVM and VIPER Interactor-Router).

Follow me

Twitter: dmlebron

iOS Engineer