简体   繁体   English

在Android MVP中,演示者应该返回一个值吗?

[英]In Android MVP, should a presenter return a value?

I try to learn MVP, I have some question to ask, should a presenter return a value? 我试着学习MVP,我有一些问题要问,演示者是否应该返回一个值?

something like this: 这样的事情:

class MainPresenter : BasePresenter<MainContract.View>(), MainContract.Actions {
    override fun getProducts (id: Int): List<Product> {
        //...
        return products
    }
}

interface MainContract {
    interface Actions {
        fun getProducts(id: Int): List<Product>
    }
}

or like this: 或者像这样:

class MainPresenter : BasePresenter<MainContract.View>(), MainContract.Actions {
    override fun getProducts (id: Int) {
        //...
        mvpView?.showProducts(products)
    }
}

interface MainContract {
    interface Actions {
        fun getProducts(id: Int)
    }

    interface View{
        fun showProducts(products: List<Product>)
    }
}

The first question we ask is, to whom should presenter return value? 我们要问的第一个问题是,主持人应该向谁返回价值? Who is interested in presenter values? 谁对演示者的价值感兴趣? Do we want to mess our business logic with view layer? 我们想用视图层搞乱我们的业务逻辑吗? Given that our business logic is inside the presenter itself, who else is interested in any data? 鉴于我们的业务逻辑在演示者本身内部,还有谁对任何数据感兴趣?

Definitely that's not our intention and diverts from MVP. 绝对不是我们的意图,而是偏离MVP。 We need to propagate the values through interfaces, usually View layer methods and pass them as arguments to other interested parties that reside in the view layer. 我们需要通过接口传播值,通常是View层方法,并将它们作为参数传递给驻留在视图层中的其他感兴趣的各方。

TL;DR: Option #2 TL; DR:选项#2

This is an opinionated answer, but I'd usually try to to inject some sort of an abstract view reference to the presenter. 这是一个自以为是的答案,但我通常会尝试向演示者注入某种抽象view引用。 Basically, an interface, where the actual implementation could be an activity, fragment, or a view, but for the presenter it would not matter. 基本上是一个接口,其中实际的实现可以是活动,片段或视图,但对于演示者而言,这并不重要。 All it knows about, is the contract presented by the interface. 所有它知道的,是界面提出的合同。

I tend to not return values from my presenters. 我倾向于不会从演示者那里返回值。 I would inject another abstraction into the presenter that gets the products. 我会向获取产品的演示者注入另一个抽象。 We usually call them interactors. 我们通常称他们为互动者。 Its responsibility is to fetch from a repository on a background thread and deliver the result on the main. 它的职责是从后台线程上的存储库中获取并在main上传递结果。 The classical way with a callback looks like this (but you should consider using kotlin coroutines instead, which would allow you to avoid the callback): 回调的经典方式看起来像这样(但你应该考虑使用kotlin协程,这样可以避免回调):

class MainPresenter(val interactor: MainInteractor) : BasePresenter<MainContract.View>(), MainContract.Actions, MainContract.Interactor.Callback {
    override fun getProduct (id: Int) {
        //...
        interactor.getProduct(product, this) // this is the callback
    }

    override fun onResult(result: Product) {
        mvpView?.showProduct(result)
    }
}

interface MainContract {
    interface Interactor {
        interface Callback<T> { fun onResult(result: T) }
        fun getProduct(id: Int, listener: Callback<Product>)
    }

    interface View{
        fun showProduct(product: Product)
    }
}

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

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