MVP架構(gòu)略解:
五華網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),五華網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為五華上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的五華做網(wǎng)站的公司定做!
M--Model,業(yè)務(wù)層(主要負(fù)責(zé)具體功能實(shí)現(xiàn))
V--View,視圖層(用作顯示)
P--Presenter,連接層(搭建Model層和View層通信橋梁)
MVP模式下,Model層和View層是完全隔離(解偶)的,兩者的通信都是通過(guò)Presenter層作為橋梁來(lái)進(jìn)行通信的,所以,Presenter層中一定含有Model層和View層具體實(shí)例(由這可以看出,當(dāng)界面改變時(shí),只需更改Presenter層中的View實(shí)例即可;同理,當(dāng)數(shù)據(jù)加載方式改變時(shí),只需更改Presenter層中的Model實(shí)例即可.
心得:
要寫(xiě)出MVP架構(gòu)代碼,主要是看M層和V層功能概述:
1.首先看View層,View層主要做顯示用,那么,寫(xiě)View層接口的時(shí)候,就要考慮下View層有多少種顯示需求,從而定義出相應(yīng)接口.
2.看完View層后,就要考慮下Model層具體業(yè)務(wù)功能的實(shí)現(xiàn)了,實(shí)現(xiàn)過(guò)程中,有可能處于耗時(shí)狀態(tài),此時(shí)的狀態(tài)就應(yīng)該通過(guò)某個(gè)接口通知到Presenter層,從而讓View層也得知,做出相應(yīng)顯示;實(shí)現(xiàn)結(jié)果成功失敗也是如此(也就是說(shuō),Model層操作結(jié)果要通過(guò)Presenter的接口讓Presenter知道,從而讓View層知道).
eg:界面只有一個(gè)TextView用作顯示,想假設(shè)從網(wǎng)絡(luò)上下載文件(模擬為加載數(shù)據(jù)),TextView在下載不同過(guò)程進(jìn)行不同的文字提示.
思路:
1.View層:主要分3個(gè)過(guò)程:下載時(shí)提示,下載成功提示,下載失敗提示.
//com.mvp.view
public interface IView
{
void onLoading();
void onSuccess(List
void onFailed();
}
2. Model層:主要進(jìn)行數(shù)據(jù)加載
//com.mvp.model
public interface IModel
{
//加載數(shù)據(jù)可能是異步操作,通過(guò)接口回調(diào)告知Presenter結(jié)果
void loadData(IPresenter listener);
}
//com.mvp.presenter
public interface IPresenter
{
void loadData();//告訴Model開(kāi)始loadData
void onSuccess(List
void onFailed();
void onDestroy();
}
以上,就將通用的MVP接口定義出來(lái)的,后續(xù)要做的就是針對(duì)特定層做出特定實(shí)現(xiàn)即可.
//com.mvp.model
public class IModelImpl01 implements IModel
{
@Override
void loadData(IPresenter listener)
{
//do download func,here pausing 10s to imitate the download behav
try{
Thread.sleep(10000);
List
data.add("imitate loading data successfully");
listener.onSuccess(data);
}catch(Exception e)
{
listener.onFailed();
}
}
}
//com.mvp.presenter
public class IPresenterImpl01 implements IPresenter
{
private IView view;
private IModel model;
public IPresenterImpl01(IView view)
{
this.view = view;
model = new IModelImpl01();
}
@Override
void loadData()
{
if(view != null)
{
view.onLoading();
}
model.loadData(this);
}
@Override
void onSuccess(List
{
if(view != null)
{
view.onSuccess(data)
}
}
@Override
void onFailed()
{
if(view != null)
{
view.onFailed();
}
}
@Override
void onDestroy()
{
view = null;
}
}
//com.mvp.view
public class MainActivity extends Activity implements IView
{
private TextView tvShow;
private IPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvShow = (TextView)findViewById(R.id.tvShow);
presenter = new IPresenterImpl01(this);
presenter.loadData();
}
@Override
void onLoading()
{
tvShow.setText("onLoading...");
}
@Override
void onSuccess(List
{
tvShow.setText("load data success:"+data);
}
@Override
void onFailed()
{
tvShow.setText("load data failed");
}
@Override
protected void onDestroy()
{
super.onDestroy();
presenter.onDestroy();
}
}