简体   繁体   English

适用于Android的MVP模式(DI-接口)

[英]MVP Pattern for Android(DI - Interfaces)

Can anybody tell me whats the difference in using Dependency Injection(eg Dagger2) to decouple the view(activity) from its presenter or to use Interfaces which the View(Activity) implements to decouple from presenter? 谁能告诉我使用依赖注入(例如Dagger2)将视图(活动)与其演示者解耦或使用视图(活动)实现的界面与演示者解耦的区别是什么?

I'm confused.. When I try to Unit Test the presenter both approaches should work. 我很困惑。当我尝试对演示者进行单元测试时,两种方法都应该起作用。 But why using a tool like dagger when simple interfaces are sufficiant? 但是,当简单的界面足够用时,为什么要使用匕首之类的工具呢? Hope that smbdy. 希望那个。 can help. 能够帮助。 Thanks 谢谢

there is not any interfere using both interface and Dagger. 使用界面和Dagger都不会造成任何干扰。 Dagger is just for DI (dependecy injection) and is used for providing different layers requirements in MVP. Dagger仅用于DI(依赖注入),用于在MVP中提供不同的图层要求。

the view interface must be used always because this is how MVP must be and views are decoupled from presenters by implementing an interface. 必须始终使用视图接口,因为这是MVP必需的方式,并且通过实现接口将视图与演示者分离。

you use Dagger to provide things from a specific layer to the other. 您可以使用Dagger从特定层向另一层提供东西。 you can completely put Dagger aside and create a helper class in your view layer which implements some helper interface and send it to the presenters through constructions all over the code but this is kinda messy and thats why they use Dagger. 您可以完全将Dagger放在一边,并在视图层中创建一个帮助程序类,该类实现一些帮助程序接口,并通过整个代码的构造将其发送给演示者,但这有点混乱,这就是为什么他们使用Dagger的原因。

look at my sample project which i used Dagger: https://gitlab.com/amirziarati/Echarge 看看我使用Dagger的示例项目: https : //gitlab.com/amirziarati/Echarge

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

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