android_MVP设计模式+举例


reference link
在这里插入图片描述

Model

In an application with a good layered architecture, this model would only be the gateway to the domain layer or business logic.
See it as the provider of the data we want to display in the view. Model’s responsibilities include using APIs, caching data, managing databases and so on.

View

The View, usually implemented[ ˈɪmplɪmentɪd] (perform/execute) by an Activity, will contain a reference to the presenter.

The only thing that the view will do is to call a method from the Presenter every time there is an interface action.

Presenter

The Presenter is responsible to act as the middle man between View and Model.

It retrieves data from the Model and returns it formatted to the View.
But unlike the typical MVC, it also decides what happens when you interact with the View.

for example:

MainActivityPresenter

Model instance should create in the Presenter class(instance)
when the Presenter was instantiate in the corresponding Activity(View)
the constructor of the Presenter will instantiate the Model and widget instance.then the Activity indirectly opereate the Model by Presenter instance,
and be careful,the Activity class implement what is inner interface member View of the Presenter(namely,Presenter.View (the inner interface))
the structure may as follows:

在这里插入图片描述
在这里插入图片描述
the updateFullName handle the model instance and the View instacne at the same time.
you may realized that the specified implementation of the updateUserInfoTextView() method depend on the Activity class

import com.mobotechnology.bipinpandey.mvp_hand_dirty.main_activity.model.User;
/**
* Created by bpn on 11/30/17.
*
* 0. In MVP the presenter assumes the functionality of the "middle-man". All presentation logic is pushed to the presenter.
* 1. Listens to user action and model updates
* 2. Updates model and view
*/
public class MainActivityPresenter {
/* contain the Model(User) and View */
private User user;
private View view;
/* constructor: */
public MainActivityPresenter(View view) {
this.user = new User();
this.view = view;
}
/* define series of methods about update the data of the Model instance
these methods could be use directly;
there,in the methods,you could operate on both the instance(of View and Model ) members ;
this is very different the methods privided by the Model class which just opreate the Model instance data.*/
public void updateFullName(String fullName){
/* operate on the Model instance:user */
user.setFullName(fullName);
/* operate on the View instance:view */
view.updateUserInfoTextView(user.toString());
}
/* similar with the updateFullName() */
public void updateEmail(String email){
user.setEmail(email);
view.updateUserInfoTextView(user.toString());
}
/* define a inner interface View,which is to regular the Activity(View)'s implementation'
more important ,the specific implementation of the methods of the inner interface View will depend on the
corresponding Activity(view); you may find that these methods don't involve the Model instance(user);
these methods may prepared for different widgets */
public interface View{
void updateUserInfoTextView(String info);
void showProgressBar();
void hideProgressBar();
}
}

User

package com.zjgsu.cxxu.memowithmindmap;
public class User {
private String fullName = "", email = "";
public User() {
}
public User(String fullName, String email) {
this.fullName = fullName;
this.email = email;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Email : " + email + "\nFullName : " + fullName;
}
}

在这里插入图片描述

MainActivity

thanks to the Presenter,the Activity(View phase) no need to operate the Model instance directly:instead ,through instantiate Presenter instance to do so.

package com.zjgsu.cxxu.memowithmindmap;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
/**
* MainActivity.java
in the view,you still need to write the logic code,further more,to cooperate with the Presenter,you need do things with presenter,as following:
* Firstly, This activity implements the MainActivityPresenter.View Interface through which it’s overridden method will be called.
* Secondly, we have to create the MainActivityPresenter object with view as a constructor.
* We use this presenter object to listen the user input and update the data as well as view .
*/
public class MainActivity extends AppCompatActivity implements MainActivityPresenter.View {
/*list the member of the Activity(view)*/
private MainActivityPresenter presenter;
private TextView myTextView;
private ProgressBar progressBar;
/* implement the methods definded in the Presenter.View inner interface */
@Override
public void updateUserInfoTextView(String info) {
myTextView.setText(info);
}
@Override
public void showProgressBar() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void hideProgressBar() {
progressBar.setVisibility(View.INVISIBLE);
}
/* create the instance of the Presenter object (often in the overridden onCreate() method)*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/* instantiate the Presenter object in the onCreate() method: */
presenter = new MainActivityPresenter(this);
/* the presenter no need to deal the view binding work */
myTextView = findViewById(R.id.myTextView);
EditText userName = findViewById(R.id.username);
EditText email = findViewById(R.id.email);
initProgressBar();
userName.addTextChangedListener(
new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
/* use the presenter instance to listen the user input and update the data as well as view
you'd know that ,this method want to modify the model's data,the it's time to call
presenter to delegate the task with method api the Presenter defined and implemented by us just before in the Activity(View) class */
presenter.updateFullName(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
/* as for methods like this ,you no need to call presenter to deal with it
becasue the operation/behaviour will not trigger the change of the model(instance,the is the user instance) */
hideProgressBar();
}
}
);
/* the similar listener case with the addTextChangedListener of userName local instance variable */
email.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
presenter.updateEmail(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
hideProgressBar();
}
});
}
/* just for the widget ProgressBar */
private void initProgressBar() {
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleSmall);
progressBar.setIndeterminate(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
Resources.getSystem().getDisplayMetrics().widthPixels,
250
);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
this.addContentView(progressBar, params);
showProgressBar();
}
}
posted @   xuchaoxin1375  阅读(4)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2023-03-17 对一个数组奇数位和偶数位上的元素分别按照升序进行排序
2022-03-17 墨刀_滚动和长屏/事件&跳转到指定页面的指定状态
点击右上角即可分享
微信分享提示