Car*_*Lee 5 c# java oop mvp design-patterns
考虑以下实现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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是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();
}
}
Run Code Online (Sandbox Code Playgroud)
哪一个更正确实现MVP模式?为什么?
我的简短回答:
我想说的是第一个。
我的长答案:
基本上 MVP 有两种变体:被动视图和监督演示者
您的伪类创建被动视图的实现。
要查看差异:请检查此处的第一个答案。它完美地描述了它们以及它们之间的区别,因此我认为没有必要在这里复制内容。
我的回答理由:
被动视图的主要思想是让视图尽可能愚蠢。它只是在发生某些用户操作时通知其呈现者,并公开访问器和修改器以从 GUI 获取和设置值。所有这些都是为了在视图级别实现最大的可测试性。
基于此,当按下按钮时,视图不应该知道它应该提供输入文本框中的值。它只应该通知演示者按钮被按下,并公开 getter 以便演示者收集它想要的任何用户输入。
归档时间: |
|
查看次数: |
795 次 |
最近记录: |