本文介绍了改善Scala中的MVP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经典强类型的MVP模式在Scala中看起来像这样:

The classical strongly typed MVP pattern looks like this in Scala:

trait IView { }

trait Presenter[View <: IView] {  // or have it as an abstract type member
  val view : View
}

case class View1(...) extends IView { ... }

case object Presenter1 extends Presenter[View1] { 
  val view = View1(...)
}

现在,我想知道有没有什么好的方法来改善,我失踪了...

Now, I wonder if there is any nice way to improve on it which I am missing...

推荐答案

关于MVP模式的好东西是它的UI代码单元可测试。

Nice thing about MVP pattern is that it makes your UI code unit testable.

建议您避免在演示者中实例化视图,并将其传递给构造函数。
这将允许你只是嘲笑查看并单元测试 Presenter

I'd suggest you to avoid instantiating view in presenter and pass it to constructor.That will allow you to just mock out the View and unit test the Presenter.

替换代码

case object Presenter1 extends Presenter[View1] { 
  val view = View1(...)
}

with

case object Presenter1(val view: View1) extends Presenter[View1] { 
  ...
}

这篇关于改善Scala中的MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 07:35