Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda