简体   繁体   English

为什么我们应该在 Android 的 MVP 模式中使用接口?

[英]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.我第一次使用MVP模式使用Kotlin制作Android应用程序。 My questions is, why do I need interfaces for View and Presenter as Kotlin provides higher order functions?我的问题是,为什么我需要 View 和 Presenter 的接口,因为Kotlin提供了高阶函数? 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)
 }
}
  1. Higher-order function costs高阶函数成本

    Kotlin official documentation on the cost of higher order functions Kotlin 官方文档关于高阶函数的成本

    Using higher-order functions imposes certain runtime penalties: each function is an object, and it captures a closure, ie 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. 2. 接口可以容纳多个函数,在使用高阶函数时需要单独的函数参数。 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要将其转换为高阶函数用法,您必须使用三个函数参数

why do I need interfaces for View and Presenter as Kotlin provides higher order functions?为什么我需要 View 和 Presenter 的接口,因为 Kotlin 提供了更高阶的函数?

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.这就是高阶函数所缺少的 - 在使用LoginPresenter.userLogin()方法时,您受到唯一类型LoginPresenter限制。

  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.随着具体的实现,代码维护和扩展变得更加困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM