VIP Architecture pattern (VIP !=VIPER)

Nirajpaul Ios
4 min readJul 22, 2021

--

Uncle Bob’s Clean Architecture: https://clean-swift.com/

The flow of VIP Architecture Pattern

Main Goal of this design pattern

  1. Testability
  2. Fixed Massive view controller
  3. Unidirectional flow of information

In theory, each scene is a structure with around 6 components:

  • View Controller
  • Interactor
  • Presenter
  • Worker
  • Models
  • Router

The view controller, interactor, and presenter are the three main components of Clean Swift. They act as input and output to one another as shown in the following diagram.

The output of the view controller is connected to the input of the interactor. The output of the interactor is connected to the input of the presenter. The output of the presenter is connected to the input of the view controller. This means the flow of control is always unidirectional.

Example: Imagine a screen with a login button. It’s a Scene that defines a structure with a VIP cycle of View Controller, Interactor, and Presenter. When the user taps the button, View Controller calls the interactor. Interactor uses the business logic inside to prepare an output (with the use of workers). Then propagates the result to the presenter. The presenter calls the VC’s method to call the router to display a new scene.

View Controller

  • Defines a scene and contains a view or views.
  • Keeps instances of the interactor and router.
  • Passes the actions from views to the interactor (output) and takes the presenter actions as input.

Interactor

  • Contains a Scene’s business logic.
  • Keeps a reference to the presenter.
  • Runs actions on workers based on input (from the View Controller), triggers and passes the output to the presenter.
  • The interactor should never import the UIKit.

Worker

  • An abstraction that handles different under-the-hood operations like fetch the user from Core Data, download the profile photo, allows users to like and follow, etc.
  • Should follow the Single Responsibility principle (an interactor may contain many workers with different responsibilities).

Presenter

  • Keeps a weak reference to the view controller that is an output of the presenter.
  • After the interactor produces some results, it passes the response to the presenter. Next, the presenter marshals the response into view models suitable for display and then passes the view models back to the view controller for a display to the user.

Router

  • Extracts this navigation logic out of the view controller.
  • Keeps a weak reference to the source (View Controller).

Configurator

  • Takes the responsibility of configuring the VIP cycle by encapsulating the creation of all instances and assigning them where needed.

Model

  • Decoupled data abstractions.

When to use this architecture

  • projects where unit testing is expected
  • long-term and big projects
  • projects with a generous amount of logic
  • projects you want to reuse in the future

Major difference b/w VIP vs VIPER

  • VIP works in unidirectional
  • VIP uses segue for the navigation
  • VIPER is bidirectional
  • VIPER Not uses segue for the navigation

Pros

  • Unidirectional flow of information
  • Easily testable due to a good separation of concerns between the components & protocol communications
  • Good modularity
  • Easier debugging while implementing a feature
  • Ready-made templates

Cons

  • Barrier to entry due to the new structure of the project
  • Boiler-plate code (Partly mitigated by the free templates that exist)

NOTE: Clean Swift is not the easiest to maintain but possibly the best to avoid coupling and be able to keep the high code coverage score.

If you want to start with the implementation of the Clean Swift architecture, you may want to use Xcode templates for Clean Swift, which is described in this blog post.

Find my git repo: https://github.com/Nirajpaul2/VIPDEMO

Great Article on VIP:

  1. https://blog.devgenius.io/clean-swift-vip-with-example-6f6e643a1a01
  2. https://clean-swift.com/clean-swift-ios-architecture/
  3. https://medium.com/@mlukacs_42903/part-1-what-is-clean-swift-vip-a-brief-overview-a43d4d66e664
  4. https://hackernoon.com/introducing-clean-swift-architecture-vip-770a639ad7bf
  5. https://zonneveld.dev/the-clean-swift-architecture-explained/

I refer to see some VIP Architecture Demo project

  1. https://github.com/Nirajpaul2/VK-Feed
  2. https://github.com/Nirajpaul2/VIP-2
  3. https://github.com/Nirajpaul2/SingaporeEyes
  4. https://github.com/Nirajpaul2/Weather-VIP
  5. https://github.com/Nirajpaul2/VIP_demo
  6. https://github.com/Nirajpaul2/CleaniTunes
  7. https://github.com/Nirajpaul2/Simple-VIP-architecture
  8. https://github.com/Nirajpaul2/vip-demo-swift
  9. https://github.com/Nirajpaul2/VIP-combine
  10. https://github.com/Nirajpaul2/IChatVIP
  11. https://github.com/Nirajpaul2/contacts-cleanswiftvip-sqlite-ios
  12. https://github.com/Clean-Swift/CleanStore

A Basic difference b/w MVC, MVVM, VIPER, VIP

Video content on VIP: https://youtu.be/yg761vPaMDs

--

--