简述
MVC:即Model,View,Controller
代码流程:
MVP: 即Model,View,Presenter;是从经典的MVC模式演变出来的升级版。
代码流程:
在MVP中各层负责的内容是这样的...
- Model层
负责访问数据,数据源可以是远端数据,也可是本地数据库内的数据.
- View层
对应的Activity、Fragment类
- Presenter
负责View层与Model层的交互
MVP与MVC的区别
MVP与MVC的最大区别在于MVC允许View层对Model层直接进行交互,而MVP则不然,MVP中Model层只能通过Presenter与View层进行交互极大程度上实现了解耦。
MVP在代码中的基本流程
个人理解:
MVP在庞大的项目结构中可能会充分发挥作用,可以由不同的人来完成不同的模块,充分的节省项目开发周期。但是应用在小项目里,却是增加了不少的类与接口,同时也增加了代码量,让本来很简洁的代码变得繁重。
实例:
大致流程
首先创建对应的接口
DemoView
DemoModel
DemoPresenter
onFunctionListener
接口创建好了之后创建对应的实现类
MainActivity
DemoModelClass
DemoPresenterClass
以及对应的实体类
DemoView接口中所做的操作
public interface DemoView {
// 开始网络请求的动画效果
void showDialogDemo();
// 关闭动画效果
void dismissDialogDemo();
// 请求成功
void MainSuccess(Object object);
// 请求失败
void MainFial(Throwable throwable);
}
DemoModel接口中的操作
// 主要是网络请求
public interface DemoModel {
void shozanen(int id, OnFunctionListener listener);
// 发现的接口数据
void search(OnFunctionListener listener);
}
DemoPresenter接口
public interface DemoPresenter {
// 用来调用M层网络请求
void dataprocessing(int id);
// 具体操作结束后关闭当前View
void onDestroy();
}
MainActivity 需要实现View的接口
public class MainActivity extends AppCompatActivity implements DemoView {
private ImageView mImageView;
private TextView mTextView;
private Button mButton;
private ProgressBar mBar;
private DemoPresenterClass mLoginPresenter;
private TextView mFial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageTV);
mTextView = (TextView) findViewById(R.id.text_name);
mButton = (Button) findViewById(R.id.ok_BT);
mBar = (ProgressBar) findViewById(R.id.progr);
mFial = (TextView) findViewById(R.id.fialTV);
mLoginPresenter = new DemoPresenterClass(this);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MainActivity", "点击事件");
mLoginPresenter.dataprocessing(1);
}
});
}
@Override
public void showDialogDemo() {
mBar.setVisibility(View.VISIBLE);
}
@Override
public void dismissDialogDemo() {
mBar.setVisibility(View.GONE);
}
@Override
public void MainSuccess(Object object) {
Log.d("MainActivity", "接收到最终的请求成功数据");
Toast.makeText(this, "网络请求成功", Toast.LENGTH_SHORT).show();
List<SearchEntity> list = (List<SearchEntity>) object;
mTextView.setText(list.get(2).getKeyword());
}
@Override
public void MainFial(Throwable throwable) {
Log.d("MainActivity", "最终接收到的失败数据");
mFial.setText(throwable+"");
}
}
Presenter实现类
public class DemoPresenterClass implements DemoPresenter, OnFunctionListener {
private DemoView mDemoView;
private DemoModel mDemoModel;
public DemoPresenterClass(DemoView demoView) {
this.mDemoView = demoView;
// 这里new出来的是Mode层的实现类
this.mDemoModel = new DemoModelClass();
}
@Override
public void dataprocessing(int id) {
Log.d("DemoPresenterClass", "准备网络请求");
mDemoView.showDialogDemo();
Log.d("DemoPresenterClass", "开始请求动画");
switch (id){
case 5:
Log.d("DemoPresenterClass", "开始网络请求");
mDemoModel.shozanen(id, this);
break;
case 1:
Log.d("DemoPresenterClass", "开始网络请求search");
mDemoModel.search(this);
break;
default:
}
}
@Override
public void onDestroy() {
mDemoView = null;
}
@Override
public void onSuccess(Object object) {
Log.d("DemoPresenterClass", "接收Model请求成功层发来的数据");
mDemoView.dismissDialogDemo();
Log.d("DemoPresenterClass", "关闭请求动画");
if (mDemoView != null) {
Log.d("DemoPresenterClass", "将请求成功的数据发送到Activity");
mDemoView.MainSuccess(object);
}
}
@Override
public void onFail(Throwable throwable) {
mDemoView.dismissDialogDemo();
mDemoView.MainFial(throwable);
Log.d("DemoPresenterClass", "接收Model层请求失败发来的数据");
Log.d("DemoPresenterClass", "异常:" + throwable);
}
}
Model实现类
// 在这个类中做对数据的请求以及数据处理,并将数据发送给Presenter
public class DemoModelClass implements DemoModel {
@Override
public void shozanen(int id, final OnFunctionListener listener) {
Log.d("DemoModelClass", "正在网络请求");
RetrofitEncapsulation.getInstance().getFunctionApi()
.getShoZane(id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ShoZaneEntity>() {
@Override
public void accept(@NonNull ShoZaneEntity shoZaneEntity) throws Exception {
Log.d("DemoModelClass", "请求成功,发送数据");
listener.onSuccess(shoZaneEntity);
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
Log.d("DemoModelClass", "请求失败,发送数据");
listener.onFail(throwable);
}
});
}
@Override
public void search(final OnFunctionListener listener) {
Log.d("DemoModelClass", "正在网络请求search");
RetrofitEncapsulation.getInstance().getFunctionApi()
.getSearch().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<SearchEntity>>() {
@Override
public void accept(@NonNull List<SearchEntity> shoZaneEntities) throws Exception {
Log.d("DemoModelClass", "请求成功,发送数据ShoZane");
listener.onSuccess(shoZaneEntities);
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
Log.d("DemoModelClass", "请求失败,发送数据ShoZane");
listener.onFail(throwable);
}
});
}
}
最后onFunctionListener
public interface OnFunctionListener {
// 成功
void onSuccess(Object object);
// 失败
void onFail(Throwable throwable);
}
效果展示:
最后的Log打印: