Java Android MVP:是否应该通知演示者所有事情?
我目前正在使用MVP架构创建我的第一个应用程序。我已经多次遇到这样的情况,视图会通知演示者一些单击事件,演示者只需在视图上调用一个方法来做一些响应。 看看这个例子:Java Android MVP:是否应该通知演示者所有事情?,java,android,mvp,Java,Android,Mvp,我目前正在使用MVP架构创建我的第一个应用程序。我已经多次遇到这样的情况,视图会通知演示者一些单击事件,演示者只需在视图上调用一个方法来做一些响应。 看看这个例子: interface ProfileView { /** * Displays the avatar photo stored at the given Uri path. * @param uriPath the Uri path which is pointing to the photos loca
interface ProfileView {
/**
* Displays the avatar photo stored at the given Uri path.
* @param uriPath the Uri path which is pointing to the photos location
*/
void displayAvatarPhoto(@Nullable String uriPath);
/**
* Sets and displays the user´s name.
* @param name the user´s name to be displayed
*/
void displayName(@NonNull String name);
/**
* Sets and displays the user´s email.
* @param email the user´s email to be displayed
*/
void displayEmail(@NonNull String email);
/**
* Sets and displays the user´s birthday date.
* @param birthday the user´s birthday date
*/
void displayBirthday(@NonNull String birthday);
/**
* Sets and displays the time when the user wants to be notified.
* @param time the time when the user wants to be notified
*/
void displayNotificationTime(@NonNull String time);
void showSelectAvatarPhotoDialog();
void showEnterNameDialog();
void showEnterEmailDialog();
void showEnterBirthdayDialog();
void showSetNotificationTimeDialog();
}
interface ProfilePresenter {
/* Lifecycle methods. */
void onCreate();
void onDestroy();
/* Called when the user clicks on the corresponding views. */
void onChangeAvatarPhoto();
void onChangeNameClicked();
void onChangeEmailClicked();
void onChangeBirthdayClicked();
void onChangeNotificationTimeClicked();
/**
* Called when the user wants to delete his current avatar photo.
*/
void onDeleteAvatarPhotoClicked();
/**
* Called when the user has selected/made a new photo for his avatar profile.
* @param uriPath the Uri path where the photo is stored, not null or empty
*/
void onAvatarPhotoMade(@Nullable String uriPath);
/**
* Called when the user has entered a name for his avatar profile.
* @param name the new name the user has entered, not null or empty
*/
void onNameEntered(@NonNull String name);
/**
* Called when the user has entered an email for his avatar profile.
* @param email the new email the user has entered, not null or empty
*/
void onEmailEntered(@NonNull String email);
/**
* Called when the user has entered a birthday date for his avatar profile.
* @param year the year the user was born
* @param month the month the user was born
* @param day the day the user was born
*/
void onBirthdayEntered(int year, int month, int day);
/**
* Called when the user has toggled the notifications option.
* @param notificationsEnabled whether or not notifications should be enabled
*/
void onNotificationsToggled(boolean notificationsEnabled);
/**
* Called when the user has entered a time when he wants to be notified.
* @param hour 0 - 23
* @param minute
*/
void onNotificationTimeEntered(int hour, int minute);
}
在充当视图的片段中,我设置了以下侦听器,这些侦听器将通知演示者用户单击了某个内容:
rowItemName.setOnClickListener(v -> mvpPresenter.onChangeNameClicked());
rowItemEmail.setOnClickListener(v -> mvpPresenter.onChangeEmailClicked());
rowItemBirthday.setOnClickListener(v -> mvpPresenter.onChangeBirthdayClicked());
rowItemNotificationTime.setOnClickListener(v -> mvpPresenter.onChangeNotificationTimeClicked());
然后,演示者将简单地调用视图的一个方法来告诉它要做什么:
@Override
public void onChangeAvatarPhoto() {
mvpView.showSelectAvatarPhotoDialog();
}
@Override
public void onChangeNameClicked() {
mvpView.showEnterNameDialog();
}
@Override
public void onChangeEmailClicked() {
mvpView.showEnterEmailDialog();
}
@Override
public void onChangeBirthdayClicked() {
mvpView.showEnterBirthdayDialog();
}
@Override
public void onChangeNotificationTimeClicked() {
mvpView.showSetNotificationTimeDialog();
}
我的问题是:这是正确的方法(如果有),还是在这种情况下让视图直接显示本例中的对话框,而不向演示者发出“不必要”的调用更好
坦克很多:)我经常遇到同样的问题,但网上有很多指向各个方向的文献。所以我想没有一个真正的答案。如果你真的想追随MVP,那么你应该做你所做的。如果你是一个理性的人,你的点击事件,只是有UI的后果,然后跳过演示者。谢谢你的回答。我已经倾向于违反MVP规则(如果有像你说的真正的规则的话),因为我看不到从这个附加的抽象层中可以得到什么好处。你的回答证实了我的想法,所以非常感谢:)编辑:错thread@Elias如果你把代码放在一个预网器中,你就可以用测试来覆盖它。此外,可能很少有业务逻辑,例如发送分析事件