<分区>
考虑以下实现 MVP 模式的伪代码:
interface Presenter {
void onSendClicked();
}
interface View {
String getInput();
void showProgress();
void hideProgress();
}
class PresenterImpl implements Presenter {
// ...ignore other implementations
void onSendClicked() {
String input = view.getInput();
view.showProgress();
repository.store(input);
view.hideProgress();
}
}
class ViewImpl implements View {
// ...ignore other implementations
void onButtonClicked() {
presenter.onSendClicked();
}
String getInput() {
return textBox.getInput();
}
void showProgress() {
progressBar.show();
}
void hideProgress() {
progressBar.hide();
}
}
这里是 MVP 模式的替代实现:
interface Presenter {
void saveInput(String input);
}
interface View {
void showProgress();
void hideProgress();
}
class PresenterImpl implements Presenter {
// ...ignore other implementations
void saveInput(String input) {
view.showProgress();
repository.store(input);
view.hideProgress();
}
}
class ViewImpl implements View {
// ...ignore other implementations
void onButtonClicked() {
String input = textBox.getInput();
presenter.saveInput(intput);
}
void showProgress() {
progressBar.show();
}
void hideProgress() {
progressBar.hide();
}
}
哪个是MVP模式更正确的实现?为什么?