[英]Confusion about MVP pattern Android
I am experience Android Developer of almost 2.5 years, who was simple writing all codes in Activity class. 我经历了近2.5年的Android开发人员,他在Activity类中编写所有代码都很简单。
Recently I came across many good things about development that will make developer life awesome. 最近我遇到了许多关于开发的好东西,这将使开发人员生活变得非常棒。
What I learned (at least to start with project) 我学到了什么(至少从项目开始)
Retrofit 改造
RxJava/RxAndroid RxJava / RxAndroid
Whats my confusion about MVP 我对MVP的困惑是什么?
There are people who follow the code style 有些人遵循代码风格
But , I follow 但是,我跟着
Many people say that Logics (if..else) should only be written in Interactor . 很多人说Logics(if..else)应该只用Interactor编写。 Your View(Activity) , Presenter should not contain (If else statement) app logic.
您的视图(活动),Presenter不应包含(If else语句)应用程序逻辑。
There many tutorials found on Medium
in which people do anything . 在
Medium
上发现了许多教程,人们可以做任何事情。 They write half app logic in Presenter , rest in Interactor 他们在Presenter中编写半应用逻辑,在Interactor中休息
I tried to follow the First Option of writing every app login in Interactor 我试图遵循在Interactor中编写每个应用程序登录的第一选项
But there are scenario where I get confused where to write app login 但有一种情况我很困惑在哪里写app登录
On two use cases, 在两个用例中,
- onActivityResult
onActivityResult
- onRequestPermission
onRequestPermission
I distinguish logic in a way that, if that logic do anything on UI - UI logic (eg: update Text, showing dialog, animation, etc.) is belong to the Activity/Fragment. 我以一种方式区分逻辑,如果该逻辑在UI上做任何事情 - UI逻辑 (例如:更新文本,显示对话框,动画等)属于Activity / Fragment。 Because you are just on that Activity/Fragment, then you may just call it right away.
因为你只是在那个Activity / Fragment上,所以你可以立即调用它。
If you need to operate any business logic, such as: update the database, network request/response, etc. You may need to call it via Presenter. 如果您需要操作任何业务逻辑,例如:更新数据库,网络请求/响应等。您可能需要通过Presenter调用它。 Eg:
例如:
presenter.getDataFromTheWeb()
- using data in 2nd Activity got from 1st Actiity via Intent.putExtras (which I always use in OnCreate() of 2nd Activity)
使用第二个活动中的数据从1st Actiity通过Intent.putExtras获得(我总是在第二个活动的OnCreate()中使用)
To retrieve data in Activity/Fragment uses getArgument() , which attached with its Activity/Fragment. 要在Activity / Fragment中检索数据,请使用附带其Activity / Fragment的getArgument() 。 So that you retrieve data in Activity class makes sense.
这样您在Activity类中检索数据才有意义。
In overall, MVP is a great way to de-couple view logic/application logic and business logic, also helps you to test your app more easily. 总的来说,MVP是解除视图逻辑/应用程序逻辑和业务逻辑的一种很好的方式,也可以帮助您更轻松地测试您的应用程序。 However using right pattern depends on particular problem you solve.
但是,使用正确的模式取决于您解决的特定问题。 If your business logic is also complex, you need Interactor pattern.
如果您的业务逻辑也很复杂,则需要Interactor模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.