Architectural Pattern - VIPER (View-Interactor-Presenter-Entity-Routing)
Tags: systemdesign, designpatterns, programing, beginners
Published: 2024-02-07 07:44:25

The VIPER is an architecture pattern that separates objects into five distinct types composed of View, Interactor, Presenter, Entity and Router. VIPER divides the logical structure into distinct layer of responsibilities.

**View:** The View layer is responsible for presenting the user interface to the user and capturing user input. It is typically implemented as UIView subclasses in iOS applications. Views should be passive and should not contain business logic. Instead, they delegate user interactions to the Presenter.
	
**Interactor:** The Interactor contains the business logic of the application. It performs tasks such as fetching data from a database or an API, applying business rules, and processing data. The Interactor should be independent of any user interface-related code and should not know about the existence of the View layer. It communicates with the Presenter to update the UI based on business logic outcomes.
	
**Presenter:** The Presenter acts as an intermediary between the View and the Interactor. It receives user input from the View, processes it (if necessary), and communicates with the Interactor to perform business logic operations. The Presenter then updates the View with the results. The Presenter is responsible for formatting data to be displayed by the View and should not contain business logic itself.
	
**Entity:** The Entity represents the data model of the application. It typically consists of plain Swift or Objective-C objects that represent the data used by the application. Entities are passed between the Interactor and the Presenter to perform business logic operations.
	
**Router:** The Router handles navigation within the application. It is responsible for creating new View controllers and presenting them to the user. The Router receives instructions from the Presenter to navigate to different screens or perform other navigation-related tasks.




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13ogcm67dm5x8riyjdu0.png)





VIPER (View, Interactor, Presenter, Entity, Router) is an architectural pattern that offers several advantages and disadvantages, as outlined below:

### Advantages of VIPER:

**Separation of Concerns:** VIPER enforces a clear separation of responsibilities among its components, making the codebase easier to understand, maintain, and test. Each component has a specific role, reducing coupling and promoting modularity.
	
**Testability:** Due to its modular structure, VIPER facilitates unit testing of individual components such as Presenters, Interactors, and Entities. Since business logic is isolated from the user interface code, tests can be written more easily, allowing for better code coverage and ensuring reliability.
	
**Scalability:** VIPER is well-suited for large, complex applications as it provides a scalable architecture. It allows developers to manage complexity by breaking down the application into smaller, manageable modules, making it easier to add new features or modify existing ones without impacting other parts of the codebase.
	
**Maintainability:** The separation of concerns and the clear division of responsibilities in VIPER contribute to the maintainability of the codebase. Changes can be made to one component without affecting others, reducing the risk of unintended side effects and making maintenance tasks more straightforward.

**Flexibility:** VIPER offers flexibility in terms of component dependencies and communication patterns. Each component communicates with others through well-defined interfaces, allowing for easy substitution or customisation of individual modules without affecting the overall architecture.

### Disadvantages of VIPER:

**Complexity Overhead:** VIPER introduces additional complexity compared to simpler architectural patterns like MVC or MVP. The presence of multiple layers and the need to define interfaces for communication between components can increase development time and effort, especially for smaller or less complex projects.

**Boilerplate Code:** Implementing VIPER requires writing a significant amount of boilerplate code, especially for setting up the communication between components and defining interfaces. This can lead to increased code verbosity and make the codebase more difficult to navigate for developers who are not familiar with the pattern.
	
**Learning Curve:** VIPER may have a steep learning curve for developers who are new to the pattern or have experience with other architectural paradigms. Understanding the roles and responsibilities of each component and how they interact with one another requires time and effort, potentially slowing down the development process initially.
	
**Over-Engineering:** In some cases, VIPER may be considered over-engineering for smaller or less complex projects. The additional layers and separation of concerns may not be necessary, leading to unnecessary development overhead and potentially complicating the codebase without providing significant benefits.
	
**Increased File Count:** VIPER typically results in a larger number of files compared to simpler architectural patterns. Each component (View, Interactor, Presenter, etc.) may have its own file, leading to a proliferation of files in the project directory. Managing this increased file count can be challenging, especially in larger projects.

_Here's a basic example of how you might implement a simple login screen using the VIPER architecture in Swift:_


**View**

```
protocol LoginViewProtocol: AnyObject {
    func displayErrorMessage(_ message: String)
    func loginSuccessful()
}

class LoginViewController: UIViewController, LoginViewProtocol {
    var presenter: LoginPresenterProtocol!
    
    // Connect your UI elements here
    @IBOutlet weak var usernameTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func loginButtonTapped(_ sender: UIButton) {
        guard let username = usernameTextField.text, !username.isEmpty,
              let password = passwordTextField.text, !password.isEmpty else {
            displayErrorMessage("Please enter both username and password.")
            return
        }
        
        let user = User(username: username, password: password)
        presenter.login(user: user)
    }
    
    func displayErrorMessage(_ message: String) {
        // Show error message to the user
    }
    
    func loginSuccessful() {
        // Navigate to the next screen or perform necessary action on successful login
    }
}
```

**Interactor**

```
protocol LoginInteractorProtocol {
    func login(user: User)
}

class LoginInteractor: LoginInteractorProtocol {
    func login(user: User) {
        // Perform login logic, such as making a network request
        
        // For demonstration purposes, assume login is successful
    }
}

```

**Presenter**

```
protocol LoginPresenterProtocol {
    func login(user: User)
}

class LoginPresenter: LoginPresenterProtocol {
    weak var view: LoginViewProtocol?
    
    init(view: LoginViewProtocol) {
        self.view = view
    }
    
    func login(user: User) {
        // Perform login logic, such as validating user credentials
        
        // For demonstration purposes, assume login is successful
        view?.loginSuccessful()
    }
}
```

**Entity**

```
struct User {
    let username: String
    let password: String
}
```

**Router**

```
protocol LoginRouterProtocol {
    static func createModule() -> UIViewController
}

class LoginRouter: LoginRouterProtocol {
    static func createModule() -> UIViewController {
        let view = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
        let presenter = LoginPresenter(view: view)
        view.presenter = presenter
        return view
    }
}
```

This code includes all the components of the VIPER architecture: View (LoginViewController), Presenter (LoginPresenter), Interactor (LoginInteractor), Entity (User), and Router (LoginRouter). Each component has its specific responsibility, and they work together to implement the login functionality.

### Summary

In summary, while VIPER offers several advantages such as separation of concerns, testability, and scalability, it also comes with disadvantages such as complexity overhead, boilerplate code, and a steep learning curve. Developers should carefully consider the specific requirements of their project before deciding whether to adopt VIPER or opt for a simpler architectural pattern.

[MVC](https://dev.to/binoy123/architectural-pattern-mvc-model-view-controller-1k5o) [MVP](https://dev.to/binoy123/architectural-pattern-model-view-presenter-mvp-28hl)  [VIP](https://dev.to/binoy123/architectural-pattern-vip-view-interactor-presenter-6hf) [MVVM](https://dev.to/binoy123/architectural-pattern-mvvm-model-view-viewmodel-2jaa)