架構模式MVP在Android中的使用,架構模式mvpandroid

來源:互聯網
上載者:User

架構模式MVP在Android中的使用,架構模式mvpandroid

前一篇文章中學習了MVC架構模式在Android中的使用,不瞭解什麼是MVC架構模式的親戳這裡 架構模式 MVC 在Android中的使用。其實GoogleAndroidTeam Dev是鼓勵開發人員利用MVC架構模式開發項目的,我們平時寫代碼也或多或少的在使用MVC架構模式開發項目,比如說Google自己退出來的Volley網路請求架構就是遵循MVC架構的。我們可以理解為Volley架構是MVC當中的模型,也就是網路資料處理這一塊,無需跟View視圖有任何關聯。也符合視圖和模型分離。可能你會覺得MVC架構已經很好用了,也能滿足任何項目開發,不錯,可是從上一篇部落格你會發現,Controller控制器和View視圖顯示是在一個類Activity中體現出來的,而Android中Activity是擔當Controller控制器的角色的,如果介面操作方式繁瑣,視圖顯示複雜,那麼我們不得不在Activity中添加更多的View視圖顯示操作,這樣自然增加了Activity的代碼量,也導致了Activity承擔的任務和邏輯處理太多,職責不清晰。這一篇我們來介紹另外一種架構模式MVP。

MVP

MPV 是從經典的MVC模式演變過來的,其基本思路都是相通的。其中M是model模型,提供業務資料;P和MVC中的C擔當的角色相似,是Presenter控制者,進行邏輯處理。V是View視圖,顯示資料。MVP與MVC有著一個重大的區別:在MVP中View並不直接使用Model,它們之間的通訊是通過Presenter (MVC中的Controller)來進行的,所有的互動都發生在Presenter內部,而在MVC中View會從直接Model中讀取資料而不是通過 Controller。

MVC架構圖
MVP架構圖

從上面的架構圖可以看出MVC和MVP最大的區別就是 Model和View之間的關係。在MVC架構中,View是可以直接讀取Model模型中的資料的,Model模型資料發生改變是會通知View資料顯示發生相應的改變。而在MVP中Model和View之間的沒有任何聯絡,是兩個完全獨立的模組,當Model模型發生資料改變時,通過Presenter通知View視圖發生相應的UI改變。因此,個人覺得:MVP才是正真的視圖和模型完全分離,也就是Model模型進行業務資料處理和View視圖顯示沒有任何關聯。

MVP for Android

在Andorid項目中,我們習慣將Activity作為MVC中的控制者來達到Model模型和View視圖分離,但是在MVP架構模式中,通常將Activity作為View視圖層,因為在MVC架構模式中Activity和View視圖顯示關聯緊密,Activity中包含大量的View視圖顯示代碼,如果哪天老闆說需要修改View視圖顯示,這時候你是不是感覺需要修改Activity中的大量代碼?這麼一來會將Activity中控制邏輯破壞,也導致Activity中承擔太多的職責。根據單一職責原則,Activity主要起到使用者互動作用,也就是接收使用者輸入,顯示請求結果。因此可以通過MVP架構模式來減輕Activity的職責。看看Android項目中怎麼實現吧!

同樣拿上一篇的天氣預報小項目來舉例子:

Model模型

和MVC架構模式一樣,Model模型處理資料代碼不變

/** * Created by xjp 2015-6-7 * 天氣Model介面 */public interface WeatherModel {    void loadWeather(String cityNO, OnWeatherListener listener);}........./** * Created by xjp on 2015/6/7. * 天氣Model實現 */public class WeatherModelImpl implements WeatherModel {    @Override    public void loadWeather(String cityNO, final OnWeatherListener listener) {        /*資料層操作*/        VolleyRequest.newInstance().newGsonRequest("http://www.weather.com.cn/data/sk/" + cityNO + ".html",                Weather.class, new Response.Listener<Weather>() {                    @Override                    public void onResponse(Weather weather) {                        if (weather != null) {                            listener.onSuccess(weather);                        } else {                            listener.onError();                        }                    }                }, new Response.ErrorListener() {                    @Override                    public void onErrorResponse(VolleyError error) {                        listener.onError();                    }                });    }}

通過OnWeatherListener介面回調將Model模型處理的資料返回給Presenter控制者。

Presenter控制器
/** * Created by xjp on 2015/6/7. * 天氣 Presenter介面 */public interface WeatherPresenter {    /**     * 擷取天氣的邏輯     */    void getWeather(String cityNO);}........../** * Created by xjp on 2015/6/7. * 在Presenter層實現,給Model層回調,更改View層的狀態,確保Model層不直接操作View層 */public interface OnWeatherListener {    /**     * 成功時回調     *     * @param weather     */    void onSuccess(Weather weather);    /**     * 失敗時回調,簡單處理,沒做什麼     */    void onError();}.........package org.rocko.demos.mvp.presenter.impl;import org.rocko.demos.mvp.model.WeatherModel;import org.rocko.demos.mvp.model.entity.Weather;import org.rocko.demos.mvp.model.impl.WeatherModelImpl;import org.rocko.demos.mvp.presenter.OnWeatherListener;import org.rocko.demos.mvp.presenter.WeatherPresenter;import org.rocko.demos.mvp.ui.view.WeatherView;/** * Created by xjp on 2015/6/7. * 天氣 Presenter實現 */public class WeatherPresenterImpl implements WeatherPresenter, OnWeatherListener {    /*Presenter作為中介層,持有View和Model的引用*/    private WeatherView weatherView;    private WeatherModel weatherModel;    public WeatherPresenterImpl(WeatherView weatherView) {        this.weatherView = weatherView;        weatherModel = new WeatherModelImpl();    }    @Override    public void getWeather(String cityNO) {        weatherView.showLoading();        weatherModel.loadWeather(cityNO, this);    }    @Override    public void onSuccess(Weather weather) {        weatherView.hideLoading();        weatherView.setWeatherInfo(weather);    }    @Override    public void onError() {        weatherView.hideLoading();        weatherView.showError();    }}

從代碼中我們可以看到Presenter控制器同時持有 WeatherModel和WeatherView對象且實現了OnWeatherListener介面取回Model模型資料,因此,WeatherPresenterImpl向WeatherModel發送資料請求,然後通過OnWeatherListener介面實現擷取請求結果,在將結果通過介面WeatherView把資料顯示到Activity擔當的View視圖中。從而達到徹底將Model和View完全分離,試想在這種情況下,如果你需要修改Model是完全不會影響View視圖代碼的修改的,同理,修改View視圖層的時候,也完全無需修改Model層。相當於Model和View互相不知道對方的存在,都是通過中間控制器Presenter來傳達通訊。

View視圖

先定義一個View視圖顯示的介面WeatherView

/** * Created by xjp on 2015/6/7. */public interface WeatherView {    void showLoading();    void hideLoading();    void showError();    void setWeatherInfo(Weather weather);}

然後實現Activity實現WeatherView介面

/** * 天氣介面 */public class WeatherActivity extends BaseActivity implements WeatherView, View.OnClickListener {    private Dialog loadingDialog;    private EditText cityNOInput;    private TextView city;    private TextView cityNO;    private TextView temp;    private TextView wd;    private TextView ws;    private TextView sd;    private TextView wse;    private TextView time;    private TextView njd;    private WeatherPresenter weatherPresenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();    }    private void init() {        cityNOInput = findView(R.id.et_city_no);        city = findView(R.id.tv_city);        cityNO = findView(R.id.tv_city_no);        temp = findView(R.id.tv_temp);        wd = findView(R.id.tv_WD);        ws = findView(R.id.tv_WS);        sd = findView(R.id.tv_SD);        wse = findView(R.id.tv_WSE);        time = findView(R.id.tv_time);        njd = findView(R.id.tv_njd);        findView(R.id.btn_go).setOnClickListener(this);        weatherPresenter = new WeatherPresenterImpl(this); //傳入WeatherView        loadingDialog = new ProgressDialog(this);        loadingDialog.setTitle("載入天氣中...");    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_go:                weatherPresenter.getWeather(cityNOInput.getText().toString().trim());                break;        }    }    @Override    public void showLoading() {        loadingDialog.show();    }    @Override    public void hideLoading() {        loadingDialog.dismiss();    }    @Override    public void showError() {        //Do something        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();    }    @Override    public void setWeatherInfo(Weather weather) {        WeatherInfo info = weather.getWeatherinfo();        city.setText(info.getCity());        cityNO.setText(info.getCityid());        temp.setText(info.getTemp());        wd.setText(info.getWD());        ws.setText(info.getWS());        sd.setText(info.getSD());        wse.setText(info.getWS());        time.setText(info.getTemp());        njd.setText(info.getNjd());    }}

因此,Activity及從MVC中的Controller中解放出來了,這會Activity主要做顯示View的作用和使用者互動。每個Activity可以根據自己顯示View的不同實現View視圖介面WeatherView。

總結

源碼地址下載:源碼地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.