Android Kotlin Mvp类委派

Android Kotlin Mvp类委派,第1张

概述所以我有以下情况:classNowActivity:AppCompatActivity(),NowScreen,NowDelegatebyNowDelegateImpl(){onCreate(...){presenter.attachView(this)}有什么办法可以将某些NowScreen方法的实现委派给NowDelegate,以便可以在presenter中执行以下 *** 作:view.c

所以我有以下情况:

  class NowActivity: AppCompatActivity(), NowScreen, NowDelegate by NowDelegateImpl(){  onCreate(...){       presenter.attachVIEw(this)  }

有什么办法可以将某些NowScreen方法的实现委派给NowDelegate,以便可以在presenter中执行以下 *** 作:

vIEw.callSomeFunc()

在NowDelegate中实现了callSomeFund().

有什么办法可以完成这样的事情吗?问题是我正在使用MVP,它将视图附加到演示者.但是在一些活动中重复了一些视图实现,因此我想将其委托给另一个类.

解决方法:

如果两个接口都实现了相同的对象,则可以将其委派给同一对象.为此,只需使该对象成为构造函数参数,例如:

class NowActivity(delegate: NowDelegateImpl): AppCompatActivity(),                         NowScreen by delegate,                         NowDelegate by delegate {   constructor (): this(NowDelegateImpl()) {}  // need this default constructor for AndroID to call... }

如果委托不能同时实现两个接口的所有功能,则可以使其成为成员,然后手动将一些功能子集委托给它.

class NowActivity(private val delegate: NowDelegateImpl):                        AppCompatActivity(),                        NowScreen,                       NowDelegate by delegate {   constructor (): this(NowDelegateImpl()) {}  // need this default constructor for AndroID to call   overrIDe fun callSomeFund() { delegate.callSomeFund() }}

这两个选项都需要您创建一个默认的构造函数,该构造函数创建用于委派的对象并将其传递给主构造函数.

在这里,它被分解为一个包罗万象的示例,该示例并非仅限于AndroID,以防其他人希望看到所有发生的事情…

示例1,将所有接口委托给同一对象:

interface CommonStuff {    fun foo1()    fun foo2()}interface LessCommonStuff {    fun bar()}class CommonDelegate1: CommonStuff, LessCommonStuff {    overrIDe fun foo1() {}    overrIDe fun foo2() {}    overrIDe fun bar() {}}class Activity1(delegate: CommonDelegate1):                           LessCommonStuff by delegate,                          CommonStuff by delegate {   constructor (): this(CommonDelegate1()) {}  // need this default constructor   // ...}

示例2,使用成员手动委派一些接口:

interface CommonStuff {    fun foo1()    fun foo2()}interface LessCommonStuff {    fun bar()}class CommonDelegate2: CommonStuff {    overrIDe fun foo1() {}    overrIDe fun foo2() {}    fun barlikeThing() {}}class Activity2(private val delegate: CommonDelegate2):                      LessCommonStuff,                     CommonStuff by delegate {    constructor (): this(CommonDelegate2()) {}  // need this default constructor    overrIDe fun bar() { delegate.barlikeThing() }}
总结

以上是内存溢出为你收集整理的Android Kotlin Mvp类委派全部内容,希望文章能够帮你解决Android Kotlin Mvp类委派所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1120862.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存