VIPER Design Pattern
By: Hazal Eroğlu
Tags: Design Patterns, Viper, iOS, App 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

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

Disadvantages of the VIPER architecture

Increases complexity in small projects. In large projects, the whole team must know the pattern well and write appropriate code. 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 :)