Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Why should we use interface in MVP pattern for Android?

I'm making an Android app using Kotlin for the first time using MVP pattern. My questions is, why do I need interfaces for View and Presenter as Kotlin provides higher order functions? Can't we just communicate using those higher order functions? Is the use of pattern without interfaces bad?

I have looked and read lots of article and tutorials but non answered my question. Is what I am doing in the code below a wrong practice? Can someone explain it to me?

In my Activity

override fun init() {

    btn_login.setOnClickListener {
        LoginPresenter.userLogin(et_emailAddress.text.toString(),et_password.text.toString()){
            if (it){
                //do something
            }else{
                //do something
            }
        }
    }
}

My Presenter

object LoginPresenter {

fun userLogin(emailId: String, password: String, completion: (Boolean) -> Unit) {
    //do something
    completion(true)
 }
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

  1. Higher-order function costs

    Kotlin official documentation on the cost of higher order functions

    Using higher-order functions imposes certain runtime penalties: each function is an object, and it captures a closure, i.e. those variables that are accessed in the body of the function. Memory allocations (both for function objects and classes) and virtual calls introduce runtime overhead.

    and if you're replacing all your interfaces with higher-order functions, you may end up with a bad performance.

2. Interfaces can hold multiple functions, for which you'll need individual function params when using higher-order functions. Consider the following case,

interface UserLoginInterface {
      fun onLoginSuccess(loggedInUser: User)
      fun onLoginFailure(error: ErrorResponse)
      fun onRedirect(someOtherObjectWithDirectives: SomeDataClass)
 }

To translate this to higher-order functions usage, You'll have to use three Function params

over 4 years ago · Santiago Trujillo Report

0

why do I need interfaces for View and Presenter as Kotlin provides higher order functions?

This is rather a common practice in software development. And while you may not use interfaces, there is a number of key points why interfaces are preferable. Off the top of my head:

  1. with interface you can have multiple implementations of it without actually caring about the concrete type of the implementation. This is what you're missing with the higher order functions - you're restricted with the only type, LoginPresenter, when using the LoginPresenter.userLogin() method.

  2. most of the design patterns is based on the separation of interfaces from their implementations. So programming into implementation rather than abstraction won't let you make use of those.

  3. you won't be able to properly unit test classes that depend on other implementations as no mocking is possible in this case.

  4. code maintenance and extension becomes much harder with concrete implementation.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!