MVP brief example

  MVP namely Model-View-Presenter, from the classic MVC evolved, their basic ideas have in common places: Handling Controller / Presenter responsible for logic, Model provides data, View is responsible for displaying.

First, the structure

FIG configuration MVP

  In the MVP, View not directly communicate with the Model, but indirectly through the Presenter, i.e. all interactions happen inside Presenter, to improve:

1
2
3
4
5
6
void View :: the onKeyDown () the override 
{ STD :: COUT << "View: mpModel-> the getData () <100?" << STD :: endl ; IF (mpModel-> the getData () < 100 ) // found because access View Model, so here there business logic code mpController-> increaseData (); }




  The difference is that, MVC is a system-level architecture, MVP is on a particular page, that is much larger than the MVP flexibility MVC (as all business code are placed in the Presenter), to implement extremely simple.

Second, the advantages and disadvantages

advantage
  • Complete separation of model and view, such modifications do not affect the view of the model
  • More efficient use of the model, because all interaction taking place inside the Presenter
  • Presenter can be used for a plurality of views, without changing the logical Presenter (ratio of change in view of the frequent changes always Model)
  • Detachable user interface (View) unit test (business logic in Presenter)
Shortcoming

  Because of rendering views on the Presenter, the so Presenter view and interaction would be too frequent. There is little need to understand that if too much Presenter rendering the view, often making it associated with a particular view too closely. Once the view needs to be changed, we also need to change the Presenter. For example, originally used to render the Html Presenter is now required for presenting Pdf, then the view is likely also need to change.

  For details, please refer to Baidu Encyclopedia --MVP .

Third, the sample code

model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class  : public AbstractModel
{
public:
Model() { mData = 98; }

int getData() override { return mData; }
void setData(int data) override
{
mData = data;
std::cout << "Model: data is updated" << std::endl;
}

private:
int mData;
};
view
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class  View :  public AbstractViews
{
public :
View (AbstractPresenter pPresenter *)
{
mpPresenter = pPresenter; // omit some control variables ... } void the display ( int Data) the override // display specific data determined by the Presenter { STD :: COUT << "View: Display" << << Data STD :: endl ; }








void the onKeyDown () the override
{ STD :: COUT << "View: Call mpPresenter to handle IT" << STD :: endl ; mpPresenter-> increaseData (); // all processing service code referred Presenter }




private:
AbstractPresenter* mpPresenter;
};
organizer
1
2
3
4
5
< 大专栏  MVP简要示例span class="line">6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Presenter : public AbstractPresenter
{
public:
Presenter(AbstractModel* pModel)
{
mpModel = pModel;
mpView = new View(this);
mpView->display(pModel->getData());

/* 为简单起见,在此处模拟用户点击了五次按钮 */
std::cout << "User: onKeyDown" << std::endl;
mpView->onKeyDown();
std::cout << "User: onKeyDown" << std::endl;
mpView->onKeyDown();
std::cout << "User: onKeyDown" << std::endl;
mpView->onKeyDown();
std::cout << "User: onKeyDown" << std::endl;
mpView->onKeyDown();
std::cout << "User: onKeyDown" << std::endl;
mpView->onKeyDown();
}
~Presenter() { delete mpView; }

void increaseData() override
{
std::cout << "Presenter: mpModel->getData() < 100 ?" << std::endl;
if (mpModel->getData() < 100)
{
std::cout << "Presenter: increaseData" << std::endl;
mpModel->setData(mpModel->getData() + 1);
std::cout << "Presenter: send the data which needs displaying" << std::endl;
mpView->display(mpModel->getData());
}
}

private:
AbstractModel* mpModel;
AbstractView* mpView;
};
主程序及运行结果
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
/* 可通过多态更换视图、模型、主持者 */
AbstractModel* pModel = new Model;
AbstractPresenter* pPresenter = new Presenter(pModel); // 视图已经内置到主持者里,亦可将其分离出来
delete pPresenter;
// ......
delete pModel; // 假设该Model的生命周期与整个软件相等

system("pause");
return 0;
}

operation result

Appendix: Complete Source Package

Fourth, the study recommended

Project Introduction

  Do list software (Android, MVP) , which is provided by Google for learning in which a sample project. README.md file them accordingly description is not repeated here. In addition, you can view other branches of learning how to use the framework to achieve the other application software.
Overview

Some suggestions to complete the white
  • After the Clone project or download the Zip decompression, open the project using Android Studio
  • Click "Make Project" under the menu bar Build, if the failure to operate the prompts until success
  • Click on the menu bar under the Run "Run 'app'"
  • To understand the actual operation of the software in a real or virtual Andrews Andrews Machine
  • MVP framework related to code analysis
    prompt
  • Experience, summed up the organization, standardized way related interface code

Guess you like

Origin www.cnblogs.com/lijianming180/p/12376148.html
MVP