ym——Android开发MVP模式(解决了View和Model的耦合)
什么是MVP呢?它又和我们常常听到的MVC有什么关系了以及区别呢?
ProgressBar控件和TextView控件,我们判断它是否有网络连接,如果有的话就隐藏 ProgressBar和跳转到MainActivity如果没有网络的话则显示ProgressBar和TextView,TextView则提示用户No internet。就这么简单的一个需求,我们看看如何用MVP模式做这个需求
首先我们看下M层是如何接口写的
<span style="font-size:18px;">package com.manning.androidhacks.hack020.presenter.model; public interface IConnectionStatus { boolean isOnline(); } </span>然后看看实现(我们主要是看MVP模式的使用,所以在此就不做网络连接的检查了,模拟一个状态)
<span style="font-size:18px;">package com.manning.androidhacks.hack020.presenter.model.impl; import com.manning.androidhacks.hack020.presenter.model.IConnectionStatus; public class ConnectionStatus implements IConnectionStatus { @Override public boolean isOnline() { // TODO: Here we should place the code to check the connectivity. return true; } }</span>然后我们在来看看M的接口
<span style="font-size:18px;">package com.manning.androidhacks.hack020.view; public interface ISplashView { void showProgress(); void hideProgress(); void showNoInetErrorMsg(); void moveToMainView(); } </span>以及M的实现
<span style="font-size:18px;">package com.manning.androidhacks.hack020.view.impl; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import com.manning.androidhacks.hack020.R; import com.manning.androidhacks.hack020.presenter.SplashPresenter; import com.manning.androidhacks.hack020.view.ISplashView; public class SplashActivity extends Activity implements ISplashView { private TextView mTextView; private ProgressBar mProgressBar; private SplashPresenter mPresenter = new SplashPresenter(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); mPresenter.setView(this); mTextView = (TextView) findViewById(R.id.splash_text); mProgressBar = (ProgressBar) findViewById(R.id.splash_progress_bar); } @Override protected void onResume() { super.onResume(); mPresenter.didFinishLoading(); } public void showProgress() { mProgressBar.setVisibility(View.VISIBLE); } public void hideProgress() { mProgressBar.setVisibility(View.INVISIBLE); } public void showNoInetErrorMsg() { mTextView.setText("No internet"); } @Override public void moveToMainView() { startActivity(new Intent(this, MainActivity.class)); } }</span>
最后我们来看看P层是如何控制他们的逻辑的:
<span style="font-size:18px;">package com.manning.androidhacks.hack020.view.impl; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import com.manning.androidhacks.hack020.R; import com.manning.androidhacks.hack020.presenter.SplashPresenter; import com.manning.androidhacks.hack020.view.ISplashView; public class SplashActivity extends Activity implements ISplashView { private TextView mTextView; private ProgressBar mProgressBar; private SplashPresenter mPresenter = new SplashPresenter(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); mPresenter.setView(this); mTextView = (TextView) findViewById(R.id.splash_text); mProgressBar = (ProgressBar) findViewById(R.id.splash_progress_bar); } @Override protected void onResume() { super.onResume(); mPresenter.didFinishLoading(); } public void showProgress() { mProgressBar.setVisibility(View.VISIBLE); } public void hideProgress() { mProgressBar.setVisibility(View.INVISIBLE); } public void showNoInetErrorMsg() { mTextView.setText("No internet"); } @Override public void moveToMainView() { startActivity(new Intent(this, MainActivity.class)); } }</span>
好,我个人理解,就是把逻辑层抽出来成P层,要是遇到需求逻辑上的更改就可以只需要修改P层了或者遇到逻辑上的大概我们可以直接从写一个P也可以,现在我看过的大多数开发把所有的东西都写在了Activity里面这样一来遇到改频繁改需求的时候,Activity里面就会被写的乱糟糟,所以想到了这个MVP模式希望能帮助到大家,大家是不是觉得MVP很不错呢?觉得不错的同学们就应用到你的实战开发中去吧~!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。