我目前正在使用MVP架构创建我的第一个应用程序。多次我已经遇到过这样的情况,即视图会通知演示者一些点击事件,演示者只需在视图上调用一个方法来做一些响应。 看看这个例子:
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());
然后,Presenter将简单地调用视图的方法来告诉它该做什么:
@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();
}
我的问题是:这是正确的方法吗(如果有的话)或者在这种情况下让视图直接显示此示例中的对话框而没有"不必要的"打电话给演示者?
坦克很多:)