这段时间总是被问到android的架构和设计模式。所以研究了一些关于MVC和MVP的知识点。
首先MVC我们都知道,就是模型层(Model)、视图层(View)、控制层(Controller)的缩写。在android中一般我们对数据的增删改查,还有实体类就属于M,layout中的所有xml文件就属于V,所有显示layout的,比如Activity、Fragment等都属于C。
MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负责显示。作为一种新的模式,MVP与MVC有着一个重大的区别:在MVP中View并不直接使用Model,它们之间的通信是通过Presenter (MVC中的Controller)来进行的,所有的交互都发生在Presenter内部,而在MVC中View会从直接Model中读取数据而不是通过 Controller。
下面模拟一个检查网络状态的例子,来说明MVP模式。进入项目首页的时候,首先检查,是否有网络,如果有就显示进度弹窗,提示正在加载数据,如果没有就显示网络不可用,提醒用户设置。
包结构:
首先Model接口中:
public interface MyModel {
public boolean hasInternet();
}
Model接口的实现类:
public class CheckNetwork implements MyModel {
private Context context;
public CheckNetwork(Context context){
this.context=context;
}
public boolean hasInternet()
{
ConnectivityManager connectivityManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getActiveNetworkInfo()!=null)
{
return connectivityManager.getActiveNetworkInfo().isConnected();
}
return false;
}
}
然后View接口:
public interface MyView {
public void showProgress();
public void hideProgress();
public void showErrorMsg();
public void jumpActivity();
}
View的实现类:
public class GuideActivity extends AppCompatActivity implements MyView {
private ProgressDialog dialog;
private TextView textView;
private MyPresenter presenter;
private Intent intent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide_activity);
textView= (TextView) findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sdkVersion = android.os.Build.VERSION.SDK;
if (Integer.valueOf(sdkVersion) > 10) {
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
} else {
intent = new Intent();
ComponentName comp = new ComponentName("com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
}
startActivity(intent);
}
});
dialog=new ProgressDialog(this,ProgressDialog.STYLE_SPINNER);
dialog.setProgress(50);
presenter=new MyPresenter(this);
presenter.setView(this);
presenter.startDoing();
TextView tv=new TextView(this);
tv.setText("正在加载数据。。。");
tv.setWidth(300);
tv.setHeight(150);
dialog.setContentView(tv);
}
@Override
public void showProgress() {
dialog.show();
}
@Override
public void hideProgress() {
dialog.hide();
}
@Override
public void showErrorMsg() {
textView.setVisibility(View.VISIBLE);
}
@Override
public void jumpActivity() {
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}
最后Presenter中:
public class MyPresenter {
private MyView myView;
private MyModel myModel;
public MyPresenter(Context context)
{
this(new CheckNetwork(context));
}
public MyPresenter(MyModel model)
{
myModel=model;
}
public void setView(MyView view)
{
myView=view;
}
public MyView getView()
{
return myView;
}
public void startDoing()
{
if(myModel.hasInternet())
{
myView.showProgress();
}else
{
myView.hideProgress();
myView.showErrorMsg();
}
}
}
可以看到,我们交互逻辑相关的处理都放在了Presenter中,具体实现放在了Model,页面显示放在了View中。当交互逻辑出现改动时,我们只需要修改Presenter,而不需要做其他的修改了。
参考: android中MVP理解