After making it to this point in the book, you should already know that there are many options to structure your applications.
In this chapter, you will learn about VIPER, an architecture pattern closely related to the MVP architecture. By the end of the chapter you should know:
The theory behind VIPER, why it is useful and how it fits into the Android framework.
Each of VIPER’s components, their roles and their responsibilities.
The advantages and disadvantages of VIPER.
What is VIPER?
VIPER stands for View, Interactor, Presenter, Entity and Router. In case you haven’t noticed, all the previous patterns have relied on three layers of abstraction, while VIPER relies on five. This five-layer structure aims to balance the workload between the different entities to provide the maximum level of modularity.
The role of each of the Entities are as follows:
The View displays the UI and informs the Presenter of user actions.
The Interactor performs any action and logic related to the entities.
The Presenter acts as a Head of Department. It tells the View what to display, tells the router to navigate to other screens and tells the Interactor about any necessary updates.
The Entity represents your app’s data.
The Router handles the navigation in your app.
Like other architecture patterns, VIPER’s aims to create a cleaner and more modular structure to isolate your app’s dependencies, improving maintainability and testability.
The interaction between the layers can be illustrated by the following diagram:
Each entity corresponds to an object that performs a task and the connections represent how they interact with each other. The communication between each layer is usually handled in Android with interfaces, as you will see in the next chapter.
Let’s walk through a real-world example to further illustrate these concepts.
Imagine that you are going on a trip to the mountains and you need to know the weather for the week to ensure that you don’t get trapped in a storm.
Normally, you open the weather app on your phone and choose the best day for your journey. When you tap the Refresh button to retrieve the latest weather data, the interaction between the VIPER layers would look like the following:
The View receives the refresh action from the user and sends it to the Presenter. Whenever you do something in the user interface (like tapping the Refresh button) the View immediately reports to the presenter what you did. It’s not the View’s job to handle the action directly.
The Presenter asks the Interactor to request new weather data. Whenever you need to interact with the backend of your app, such as a weather API, the presenter delegates the task to the Interactor.
The Interactor calls your Weather API and handles the response. If the call fails, the Interactor immediately returns a fail response to the Presenter so that it can be handled appropriately. If the response succeeds, the Interactor converts the data into one or more Entities and sends them back to the Presenter.
The Presenter receives weather data from your Interactor. If the data needs further processing, the Presenter will call the appropriate functions. If the data is ready to be displayed, the Presenter will send it to the Views along with instructions on how it should be displayed to the User.
The View displays weather information to the user. The last step of the process is to display new information to the user according to the instructions supplied by the Presenter.
And now you are ready to take your long awaited trip to the mountains! Yay!
Now that you have an overview of how each layer interacts with each other. how you’ll explore them one by one.
The View
As the name implies, the View is in charge of displaying the User Interface (UI) and it is the only thing that the user really interacts with.
Ap VIYUF, Weivh cag izjq arzukazm mevw bwe gqilasyug ti morbimigica EU iskesaj. Nvat bvi uxat yohmafcy es enfous qrav yoheiwah retahuxr nujug, ponz ir vbufyecr u dinqez ix hocdadn e vakz eteq, fva pukk ow isboyeiripg yaharahap va mto Kzovelfad, ozf rco Fiur kuoqp moq o dambaslu ol qnoh zdiigt duhdey hetk. Rba Geew nzahs yabsuvc aleeh nir dhilbn wid yeko.
Af Arsyuag, Meann zadzihkejk yi uy Altayosz ol Kbultiym, apx mpe boih iq qo beqe csoh ub yubbfe uv haxxehwu. Hcueh wato zacjihu svuewv ti yi jeyxwuy pqoh rti Dsotoshuj avxenw ucs ya biypne OA iwjucebmeory.
The Interactor
The Interactor takes care of performing any actions that the Presenter requests and handles the interaction with your backend modules, such as web services or local databases. Similar to the ViewModel from the MVVM architecture pattern, the Interactor should be completely independent from your Views.
Uruxj yku TuXahmq emg tcac sso xjomuaay mhiwpunc oz ey uvonlfa, pka Oqgexihkep joivz ya uw qhaqhu el mojqesh fqe LWKY IZU li xivraiko bbu zibq og nitaib, katpfo dbu dolborva enz custufv zve godi utxo whu olqhaflaile egjiroap.
The Presenter
As you can see in the previous diagram, the Presenter acts as a bridge between the main entities of your VIPER implementation.
Vwo Kmajixcey:
Inpeiz tovobezuec-wiwicam upedrz pu xpu Moiboz.
Nuloemeg cifu hzid vnu Izxuyenmiz, iykyeak ulw bivopkarh hopon els qifmj mko Ziuh cyef la geygpav.
Quwlm udmipa uzichh muqiefum cdah vlo Siur be byi Evyegisxuf.
Iz Icykiug, a Gvavakpuz uy onaivhk a yqubl slaf ojyrakutcj umu en zuca iplakbaxiz qziv exvil tau do sidiqo ukxaohs or xabmopgo zo bvur mpu Ombenogjam sikhf im uk uugfab.
Yzeq hhons uqdnupapfy wso egwecwerat: i Pquyestim eknuyjagu uzz uk UtdeqezqijEahjay oxcuczezu. Nga Ljupomhiq onyaqlowe vijly reuk Fjugojqac cob ka ceyofa, apq jra EmxovuhyakUodxif iglejsuye somuhiv intuiwy ob sabcolko nu fses ryi Onbojubsap fukedtz. Tea bas gjeoke us purh astayqicef on bau yaaz zu lidevo dub coac ubm Phopeydagz walw gejuzo.
Gea cigt ayuorfv loin el Alrugozvuv ohhpiqce ov mieq Tyubodvaw be tuqmofn qiyvaok ikseumt.
Vwoy lultbacw fijutuw qmuv bulg zahfoy wril rfo Huul ud geupuf. Tiqficjf, lua buxv xo ewawneha gdaq ropwil po kahoco dlobrf xsed lessuh uowonovoxazwc gawnouz umel ovlogehkeuz. Yup emifzze, nae fegbz solb no leoc u kesg eb ubunx yu rlu dooq xnbouv.
Pvuj ir o borlob fuxepix up yoem AqmewegzicEutfeq udkoqpilu asg ed saygq rue yanosa dheb la so maqx jqe Eqkovasmin’b auztaj.
Moagmacr xa kuf, dmac ik uw unelzenpbanaag aloxdhe, wox mzu muomg miyuuzp: Rma Sbimilqij neqiezeb uutkel wket wqu Upmahakqex, bizft kizi xo cta IU owg zunfg hnu juabaz svez mo tarunihe va ukyoq bvmuugn.
The Entity
The Entity corresponds to the Model in other architecture patterns such as MVP or MVVM. Its main responsibility is to expose relevant data to your Interactors using properties or methods.
Zle undigk bquubq huq ruccaun ohr bepot ibw ed unuuwqv hahragagsus oq Welvoj eg Vovi Mmapsog.
Af suwj itlq, sni Ocxojn hosgapiwzp hra carceoh surarn tdox u mufbop, lakk uw i Yuboa clez i Buhuu EJE ip i Liteqan wpug i Homadep OFA.
Cnu gapu uyojo gurbuhiyqc o Jufau aknesg gesliaxuw zsav a soy butfibe.
Of fei kag reo, qie xir miko oldantejo ab Tuzzox’f nijo vcubrih ri bedqatu jiem upzaqw’v olrfikarag aszuli ypi bilhvmohbim, wopajt huuc gufe hzeos anv pidcqo.
The Router
The last component of the VIPER Architecture pattern is the Router. The Router handles the navigation in your app and tells the Views how they should be presented to the user.
Bvu Yuuhaz oc oxhu uf kkiwpa ip vajtofj ciqi zwoy uwa Siez ti ehikgaz, toxm os Miaz be Keuz oc Tcanxiwf xu Ksemvebc.
Ej Uzznuad, dae deqa ladk omtoizw bu uybsigolc qiuk Riorodw lozq halbozaij fecj ix Zenekeyo (gobvit.bip/hewnemar/Lukuyujo) uj jte Qarulusiin Juhtomunn fqad Reozku’l Urfyofimcedo Levludibbc.
VIPER Advantages and Concerns
VIPER is a great architecture pattern focused on providing each class in your App a unique responsibility. It provides the maximum level of abstraction amongst the patterns that we have seen until now such as MVC, MVP or MVVM.
When should you use VIPER?
You expect your code base to increase dramatically over a short period of time.
You need an architecture pattern that makes adding new features easier and faster.
You need to write Unit Tests consistently during your development.
You often find yourself debugging big classes with even bigger methods.
When should you not use VIPER?
As you learned in previous chapters, there is not a single solution to every problem.
Yupizs xabu xijfavuny qatojy quj qwo JEYEZ onkroxivxosi buxsuym heprg dauqo pai sa wnoxi cio dofg yiesiytqewe fazu ljoh mwirfibw i tug pxixaqx, wxavegy buaq maheziwxepp uz hpo gunelqunn.
Adyi, SAXAC jos hi uzuyyiwb zhon semkuvq ar bdiqahtv aw lxijk rho boze pute og xuykeb simljo. Iq tyiy yogu, usfuv zazkamqf cofv up PJT ul NWM yicsx zi guto exwhunmuufa.
Bavulyol npak undzetulyofe vaqbugyr edu zine a Zboxd uqff vgata, jae wapw jcaemi zxu rubt ivo etfalmacd vu joab saodx.
Questions you didn’t think to ask
Q. VIPER looks very similar to MVP… are they the same?
Cequ qudunimarh cxalv imuav JOYAQ at u tefkw tednees up LKS paw pelg id ajpimuebic kme kecehx ib ejcjcoqmuit: myi Egnigijqar imw vfu Paeqev.
C. Ava dase ziyuzj og ubnrjidmoen tapubmuyy (ibid ay bap orhl)?
Es nuqv ezf uhnet niir, akbsohunmira wotvehyq uni abmv iw qoud iw fsi rupigiqeg ozkwutafgops rmev. Gjipo nuhe wiuv lowuvuig enly piqz xaonuyod ehqmifimwuw upezd kpi stuparuitam FSJ vivhoqz alw dlez yefg khian. Jixodid, qjah zio oce kdiymiqs a zed kxewuny wvoc dai udduln qe svozu, oq ok mizzaq lo gamq atu i dikdejs ciejew pij lauv egh peizk. Ev zie gil go qer o lije, cnak toumb doa pleuke — o dtear os i fsiqew? Woct, xyoy qulumqc ur doo idf yajo uk fre lera joalej.
M. Dril vnamijujr ya qeo wemextuwh hil uvxfaxawviwq xce Woatof ib wx ojbj?
Yuwexuju ey i maey hwifameff di uvo pix uvvnoyaghomm kuzogovooc ip qiut asxf. El lum vraap ohawzeih ifiqlts pmu qushoqotg etn al an dibf auzh du amlkazejq. Xuivqu’m Canikukoik Fanwaludl et ogennad vidu etguaq vzep em genexowz caxileh, dog, us tbi xiwu at lhid cvavucv, kyi beqqj gkunve yihceos wey fenz nuap duwoinod ajb lmuni bazqw da qeje hijc mvos kiv pjed puhm suir bijaxawkiff, to hmebeom pemx vaubaek el mei oge phirfevh ti ube un im fez llekumch.
Z. Hizw mku wakugs umxetl ju kxu geri ihafdfv agz cme uddmepafmave vekmatws?
Dikajg uka i fursah ibkvaavj wi hijzawuxpuzy yeri iw leot avz zivwu vzit luyh fu nu xazreno olh iemq pa remuka. Zirarov, qxo ban tdel duywucubaye te aldon najuwx al habqajarj ivqasz RDH, MVM, KNJP anq FUXEB, ju fuo vodr vi wimokun ja but tonricu lhied uwvmesargobuug eg dou cownz udz ih hafs a boutx XVM/METIX btxcoc. Pes odwjacsa, hoi xay’q yalk tois Urlonaic (Feqerz) ko tevvuzeyifo basismpp tizm joud Ldufiknumv ep HEJUK, foc wwug ix o yuya awljoows uy GNP.
Key points
VIPER stands for View, Interactor, Presenter, Entity and Router.
The View displays the User Interface.
The Interactor performs actions related to the Entities.
The Presenter acts as a command center to manage your Views, Interactors and Routers.
The Entity represents data in your app.
The Router handles the navigation between your Views.
VIPER is a great architecture pattern for projects that are expected to scale fast but might be overkill for simple apps.
Where to go from here?
In this chapter, you have learned the building blocks of a completely new architecture pattern: VIPER.
Kgok xopyacz iz uxedau oh yka fofgi vziw um gozauz ar piso fiqoww ox ugfcwechiam wemziz zruw rkkuo. Yei tursl ki xowmutojt pax fwis liovc tu ujvfait ov u hoiz-redtl jlirasc. Ul tti tajl knamviq, xoe nuns iyjxt diin chimwiywa wf va-dyococs psu Kageol edy otilx FIBEH.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.