android MVP模式簡介

來源:互聯網
上載者:User

標籤:android mvp model view presenter



MVP架構略解:

M--Model,業務層(主要負責具體功能實現)

V--View,視圖層(用作顯示)

P--Presenter,串連層(搭建Model層和View層通訊橋樑)


MVP模式下,Model層和View層是完全隔離(解偶)的,兩者的通訊都是通過Presenter層作為橋樑來進行通訊的,所以,Presenter層中一定含有Model層和View層具體執行個體(由這可以看出,當介面改變時,只需更改Presenter層中的View執行個體即可;同理,當資料載入方式改變時,只需更改Presenter層中的Model執行個體即可.


心得:

要寫出MVP架構代碼,主要是看M層和V層功能概述:

1.首先看View層,View層主要做顯示用,那麼,寫View層介面的時候,就要考慮下View層有多少種顯示需求,從而定義出相應介面.

2.看完View層後,就要考慮下Model層具體業務功能的實現了,實現過程中,有可能處於耗時狀態,此時的狀態就應該通過某個介面通知到Presenter層,從而讓View層也得知,做出相應顯示;實現結果成功失敗也是如此(也就是說,Model層操作結果要通過Presenter的介面讓Presenter知道,從而讓View層知道).



eg:介面只有一個TextView用作顯示,想假設從網路上下載檔案(類比為載入資料),TextView在下載不同過程進行不同的文字提示.

思路:

1.View層:主要分3個過程:下載時提示,下載成功提示,下載失敗提示.

//com.mvp.view

public interface IView

{

void onLoading();

void onSuccess(List<String> data);//資料下載成功後,進行顯示

void onFailed();

}

2. Model層:主要進行資料載入

//com.mvp.model

public interface IModel

{

//載入資料可能是非同步作業,通過介面回調告知Presenter結果

void loadData(IPresenter listener);

}

//com.mvp.presenter

public interface IPresenter

{

void loadData();//告訴Model開始loadData

void onSuccess(List<String> data);

void onFailed();

void onDestroy();

}



以上,就將通用的MVP介面定義出來的,後續要做的就是針對特定層做出特定實現即可.


//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<String> data = new ArrayList<String>();

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<String> data)

{

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<String> data)//資料下載成功後,進行顯示

{

tvShow.setText("load data success:"+data);

}

@Override

void onFailed()

{

tvShow.setText("load data failed");

}

@Override

protected void onDestroy() 

{

super.onDestroy();

presenter.onDestroy();

}

}





本文出自 “whatever957” 部落格,請務必保留此出處http://whatever957.blog.51cto.com/6835003/1832694

android MVP模式簡介

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.