VIPER Design Pattern

Hazal Eroğlu
3 min readDec 12, 2020

Design patterns are techniques that minimize code duplication and standardize a common way of writing code.

There are many design patterns we use in iOS applications. But as the projects grows, the patterns that we use for example MVC or MVVM become inadequat. The most compatible pattern for iOS is considered to be VIPER. Now, we will examine design pattern called VIPER for iOS development.

What is VIPER?

VIPER has five different classes with distinct roles. No class go beyond its sole purpose. These classes;

View : Class that shows the application interface to the user. It waits for content to arrive from Presenter and It alerts the Presenter according to user actions.

Interactor : Interactor is a class that mediates between the presenter and the data. So business logic are found here. The operations performed here should be completely independent of the UI.

Presenter : It is only class that communicate with all the other components. It directing data between the view and interactor, taking user actions, calling to router to move the user between views.

Entity : Contains data models related to the application that used by Interactor. This section only collaborates with Interactor.

Router : Does the wire-framing. Listens from the presenter about which screen to present and executes that. Also, transition animations are found in this layer. The router only communicates with Presenter.

Viper is a delegation driven architecture. So, most of the communication between different layers executes through delegation. OK, we will talk about delegation in example that we examine. If we say the advantages and disadvantages of this design pattern;

Advantages of the VIPER architecture

  1. Increases the testability of the code.
  2. It ensures the reusabilty of the application as it divides the application into parts.
  3. Increases the readability of the code.
  4. It makes it easy to add new features.

Disadvantages of the VIPER architecture

  1. Increases complexity in small projects.
  2. In large projects, the whole team must know the pattern well and write appropriate code.
  3. It can create complexity as there are many folders and delegates in the project.

Now, we will examine VIPER design pattern with an example

We said something that VIPER is a delegation driven architecture above. Therefore, one layer calls another through a protocol. A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task.

Let’s take a look at our project :)

It’s our LoginPageInterfaces

View has a reference of LoginPagePresenterInterface to access the Presenter. In it’s button action function it calls the function loadLoginData() of the protocol.

Presenter has a reference of LoginPageInteractorInterface to access the Interactor. Inside loadLoginData() presenter tells the interactor to fetch some live news data.

Interactor implements getToken() function. This function makes a network call and fetch data.

If the network call successfully have fetched the data it calls the loadLoginDataSuccess() function. If not, calls the loadLoginDataFailed() function.

According to the data returned from Interactor to Presenter, View implements the necessary functions. In these two functions view populates the view with the fetched data or the error.

In VIPER, Router layer listens from the presenter about which screen to present and executes that. A presenter calls the router to create a new module. Then router first initiate all the layer class and returns the module.

Thanks for your time

Happy coding :)

--

--

No responses yet