VIPER Architecture
By: Ahmed Lotfy
VIPER Overview
VIPER Architecture approach
for application development
Clean Architecture divides
logical structure into distinct
layers of responsibility.
VIPER Main Goals
Resolve problems. Increase of test coverage for
the Presentation level.
Using Single Responsibility Principle
History
●08 - 2012
⚪An article The Clean Architecture by Robert Martin.
●12 - 2013
⚪An article Introduction to VIPER
●06 - 2014
⚪Issue #13. objc.io Architecting iOS Apps with VIPER
●New architecture, introduced on 2014
at Mutual Mobile.
History
“We found that writing tests for iOS apps was difficult.
We decided that if we were going to improve the way we
test our software, we would first need to come up with a
better way to architect our apps. We call that method
VIPER”.
Note
“VIPER is not the list of rules and templates. It's
the list of recommendations how to build flexible,
testable and reusable architecture.”
What is the problem?
Old Paradigm
My project is getting larger..
Best Architecture?
Big Project
Massive View Controller
●What goes into View Controllers?
Application Flows
Transitions
Massive View Controller
Large files (+1000 lines) High complexity Unstructured code
Maybe VIPER Will Solve The Problem
VIPER Architecture
VIPER Components
V(iew) I(nteractor) P(resenter) E(ntity) R(outer)
VIPER Components
Components
act like plugins
to use cases.
VIPER is a way of describing:
○ What is the role of each of these
components?
○ How they can interact with one
another?
Entity
Contains basic model objects.
Don’t be surprised if your entities are just data
structures.
Entities are only manipulated by the Interactor.
Example
Interactor
A single use case.
Business logic to Entities to a specific task.
Should be independent of any UI.
The Interactor never passes entities to the presentation
layer.
Interactor
The same Interactor could be
used in an iOS app or an OS X
app.
Easy to develop using TDD.
Data Manage
Data Manager Role:
⚪ Building fetch requests.
⚪ Store/fetch data from
database.
This allows the Interactor to
focus more on application logic
and not have to know anything
about how. Entities are
gathered or persisted.
Interactor Example
Example
View
Passive Component (UILabel, UITableView, ..).
Determine how the content is displayed.
The Presenter give it contents to display.
It never asks the Presenter for data.
Wireframes (Routing)
Own the UINavigationController and UIViewController
Create a View/ViewController and installing it in the
window.
Contain navigation logic for describing which screens are
shown in which order.
Routes from one screen to another are defined here.
Example
Presenter
●Contain view logic.
●Do not know about UILabel, UIButton, etc.
●Know about the content and when it should be displayed.
Presenter
Contain the logic to react to user inputs by requesting
new data from the Interactor.
Receive results from an Interactor and Prepare content
to a form that is efficient to update the UI.
Entities are never passed.
Simple data structures passed.
Use the wireframe to perform the navigation and
present the UI.
Example
Example
Presenter and View
The communicate between Presenter and View by a
higher level of abstraction (a protocol) expressed in
terms of its content,
and not how that content is to be displayed.
Presenter and Wireframe
●The Presenter knows:
⚪When to navigate to another screen.
⚪Which screen to navigate to it.
●The wireframe knows:
⚪How to navigate.
⚪Handle navigation transition animations.
Presenter and Wireframe
Responsibility for Routing is shared between two
objects: the Presenter, and the wireframe.
Together, they describe a route from one screen to the
next.
Example
VIPER and Storyboard
VIPER tends not to use segues.
The danger with segues is they make it very difficult to
keep the separation between UI and application logic –
intact.
Using VIPER to Build Modules
A screen or set of screens tends to come together as a
module.
A module can be described as a feature.
Good simple way to organize code.
Easy to find a class exactly where you
expected to look for it.
Designing your App as a set of Modules
Modules can have very clear and well-defined
interfaces, as well as be independent of other
modules.
This makes it much easier to add/remove features, or to
change the way the interface presents various modules
to the user.
Modules
A module might include a common application logic
layer of entities, interactors, and managers that can
be used for multiple screens.
Depends on the interaction between these screens and
how similar they are.
A module could just as easily represent only a single
screen.
Testing with VIPER
Separation of concerns makes it
easier to adopt TDD.
The Interactor contains pure logic.
The Presenter contains logic to
prepare data for display.
They are independent of any UI
Generarted Tools
iOS Code generation:
Generamba ViperCode
VIPER gen
Boa
VIPER vs. MV(C/P)
Advantages
Structure:
Lighter and Neat Code!
Stricter classes.
Isolate dependencies.
Separated concerns.
Conforms to “Single Responsibility Principle”.
Advantages
Help Team:
Helps developer to be more explicit about separation of
code.
Excellent scalability of the tasks among developers.
●Easier to maintain.
Test:
Easier to test the interactions at the boundaries
between layers (No excuse for making tests).
Advantages
Test:
Increase of testability for the application presentation
layer.
Modules are independent from each other. It makes the
separate development environment and increases the
code reusability.
Main architecture approaches are defined. So it's much
easier to add new developer into the team or move
project to another team.
Disadvantages
VIPER looks difficult at first, especially for
developers without the team work
experiences on large projects.
Highly increases of the number of class in the
project, as well as the complexity of
creating the new module.
Some principles don't work with UIKit out of
the box (Segue).
References
https://www.objc.io/issues/13-architecture/viper/
Swift example.
https://github.com/rambler-digital-solutions/The-Book-
of-VIPER/blob/master/english/introduction-to-
viper.md
http://www.slideshare.net/instinctools_EE_Labs/viper-
architecture-59871979
http://www.slideshare.net/HendyChristianto/introducti
on-to-viper-architecture
References
#8 VIPER to be or not to be?
https://brigade.engineering/brigades-experience-using-
an-mvc-alternative-36ef1601a41f#.a0skcve7l
https://github.com/rambler-digital-solutions/The-Book-
of-VIPER/blob/master/english/contents.md
https://8thlight.com/blog/uncle-bob/2012/08/13/the-
clean-architecture.html
Android Templates.
Thank you

VIPER Architecture

Editor's Notes

  • #4 VIPER - Architecture approach for application development(particularly iOS) , based on the Clean Architecture by Robert C. Martin (Uncle Bob).
  • #5 Conform to the Single Responsibility Principle (Every module or class should have responsibility over a single part of the functionality). Presentation level: UI layer, view layer In simple terms, it is a layer which users can access directly (such as a web page, or an operating system's GUI). The Single Responsibility Principle (Every module or class should have responsibility over a single part of the functionality).
  • #6 An article Introduction to VIPER by MutualMobile. Issue #13. objc.io Architecting iOS Apps with VIPER by MutualMobile publishes books on advanced techniques and practices for iOS and OS X development Mutual Mobile is an emerging tech agency that builds breakthrough products for a more connected world.
  • #9 Big and Long-Term Project What is the best Architecture?
  • #11 UIViewController class defines the methods and properties for managing your views, handling events, transitioning from one view controller to another, and coordinating with other parts of your app. Data sources for Views (UITableViews). Business Logic . Application Flows (Manage the life cycles). Transition between view controller.
  • #12 Large files (+1000 lines) . A lot of unstructured code. High complexity. Untestable code.
  • #16 In software and systems engineering, a use case is a list of actions or event steps, typically defining the interactions between a role (known in the Unified Modeling Language as an actor) and a system, to achieve a goal. The actor can be a human or other external system. Use cases are a type of textual requirements specification that capture how a user will interact with a solution to achieve a specific goal. They describe the step by step process a user goes through to complete that goal using a software system.
  • #17 Used by the Interactor.
  • #19 Use cases are a type of textual requirements specification that capture how a user will interact with a solution to achieve a specific goal. They describe the step by step process a user goes through to complete that goal using a software system. Fetch Save specific data. Sort data
  • #20 The design conventions, APIs, and programming patterns are the same. You’ll use the same Foundation classes - collection sets, strings, numbers… nothing new here at all. An NSArray is still an NSArray. Of course, when it comes to the UI, the complexity starts to increase. In iOS you’re used to dealing with a straightforward view progression. The user always moves from one view to the next, and as developer you control that linearly. On the desktop, you may begin with multiple windows, progress with spawning new or closing windows, respond to events from the menu bar, handle undos, and so on. In short, there are a lot more things to keep track of.
  • #21 Where should this networking take place? It’s up to the Interactor to initiate a network operation, but it won’t handle the networking code. What should be responsible for initiating it? It will ask a dependency, like a network manager.
  • #24 The View is an abstract interface, defined in Swift with a protocol. A UIViewController or one of its subclasses will implement the View protocol.
  • #27 Consists logic to drive the UI. Do not know about the existence of UILabel, UIButton, etc.
  • #28 Set image from car resuts to car view
  • #35 Storyboards are a great way to implement the layout for your user interface using Auto Layout.
  • #38 The module’s Presenter usually implements the module interface. When another module wants to present this one, its Presenter will implement the module delegate protocol.
  • #39  A concern is a set of information that affects the code of a computer program. In computer science, separation of concerns (SoC) is a design principle for separating a computer program into distinct sections, such that each section addresses a separate concern
  • #50 Need more sreenshots.