架構模式 MVC 在Android中的使用,mvcandroid
算來學習Android開發已有2年的曆史了,在這2年的學習當中,基本掌握了Android的基礎知識。越到後面的學習越感覺困難,一來是自認為android沒啥可學的了(自認為的,其實還有很多知識科學),二來網路上的很多架構已經幫我們做了太多的事情了,我們只需要畫畫UI就可以了,感覺Android開發沒有太多的技術含金量。最近閑來無事,開始總結之前學過的知識點,想著是否應該學點其他的東西呢?總不能局限於Android基礎知識吧。慢慢的探索發現在大的項目工程中,一個好的架構,好的設計模式,能減少很大的工作量。因此接下來兩篇部落格來學習一下Android中常用的兩種架構設計模式 MVC和MVP。
MVC概念
MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫,一種軟體設計典範,用一種商務邏輯、資料、介面顯示分離的方法組織代碼,將商務邏輯聚集到一個組件裡面,在改進和個人化定製介面及使用者互動的同時,不需要重新編寫商務邏輯。其中M層處理資料,商務邏輯等;V層處理介面的顯示結果;C層起到橋樑的作用,來控制V層和M層通訊以此來達到分離視圖顯示和商務邏輯層。說了這麼多,聽著感覺很抽象,廢話不多說,我們來看看MVC在Android開發中是怎麼應用的吧!
MVC for Android
在Android開發中,比較流行的開發架構模式採用的是MVC架構模式,採用MVC模式的好處是便於UI介面部分的顯示和商務邏輯,資料處理分開。那麼Android項目中哪些代碼來充當M,V,C角色呢?
接下來我們通過一個擷取天氣預報資料的小項目來解讀 MVC for Android。先上一個介面圖:
Controller控制器
package com.xjp.androidmvcdemo.controller;import android.app.Dialog;import android.app.ProgressDialog;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import com.xjp.androidmvcdemo.R;import com.xjp.androidmvcdemo.entity.Weather;import com.xjp.androidmvcdemo.entity.WeatherInfo;import com.xjp.androidmvcdemo.model.OnWeatherListener;import com.xjp.androidmvcdemo.model.WeatherModel;import com.xjp.androidmvcdemo.model.WeatherModelImpl;public class MainActivity extends ActionBarActivity implements OnWeatherListener, View.OnClickListener { private WeatherModel weatherModel; 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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); weatherModel = new WeatherModelImpl(); initView(); } /** * 初始化View */ private void initView() { 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); loadingDialog = new ProgressDialog(this); loadingDialog.setTitle("載入天氣中..."); } /** * 顯示結果 * * @param weather */ public void displayResult(Weather weather) { WeatherInfo weatherInfo = weather.getWeatherinfo(); city.setText(weatherInfo.getCity()); cityNO.setText(weatherInfo.getCityid()); temp.setText(weatherInfo.getTemp()); wd.setText(weatherInfo.getWD()); ws.setText(weatherInfo.getWS()); sd.setText(weatherInfo.getSD()); wse.setText(weatherInfo.getWSE()); time.setText(weatherInfo.getTime()); njd.setText(weatherInfo.getNjd()); } /** * 隱藏進度對話方塊 */ public void hideLoadingDialog() { loadingDialog.dismiss(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_go: loadingDialog.show(); weatherModel.getWeather(cityNOInput.getText().toString().trim(), this); break; } } @Override public void onSuccess(Weather weather) { hideLoadingDialog(); displayResult(weather); } @Override public void onError() { hideLoadingDialog(); Toast.makeText(this, "擷取天氣資訊失敗", Toast.LENGTH_SHORT).show(); } private <T extends View> T findView(int id) { return (T) findViewById(id); }}
從上面代碼可以看到,Activity持有了WeatherModel模型的對象,當使用者有點擊Button互動的時候,Activity作為Controller控制層讀取View視圖層EditTextView的資料,然後向Model模型發起資料請求,也就是調用WeatherModel對象的方法 getWeathre()方法。當Model模型處理資料結束後,通過介面OnWeatherListener通知View視圖層資料處理完畢,View視圖層該更新介面UI了。然後View視圖層調用displayResult()方法更新UI。至此,整個MVC架構流程就在Activity中體現出來了。
Model模型
來看看WeatherModelImpl代碼實現
package com.xjp.androidmvcdemo.model;/** * Description:請求網路資料介面 * User: xjp * Date: 2015/6/3 * Time: 15:40 */public interface WeatherModel { void getWeather(String cityNumber, OnWeatherListener listener);}................package com.xjp.androidmvcdemo.model;import com.android.volley.Response;import com.android.volley.VolleyError;import com.xjp.androidmvcdemo.entity.Weather;import com.xjp.androidmvcdemo.volley.VolleyRequest;/** * Description:從網路擷取天氣資訊介面實現 * User: xjp * Date: 2015/6/3 * Time: 15:40 */public class WeatherModelImpl implements WeatherModel { @Override public void getWeather(String cityNumber, final OnWeatherListener listener) { /*資料層操作*/ VolleyRequest.newInstance().newGsonRequest("http://www.weather.com.cn/data/sk/" + cityNumber + ".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(); } }); }}
以上代碼看出,這裡設計了一個WeatherModel模型介面,然後實現了介面WeatherModelImpl類。controller控制器activity調用WeatherModelImpl類中的方法發起網路請求,然後通過實現OnWeatherListener介面來獲得網路請求的結果通知View視圖層更新UI 。至此,Activity就將View視圖顯示和Model模型資料處理隔離開了。activity擔當contronller完成了model和view之間的協調作用。
至於這裡為什麼不直接設計成類裡面的一個getWeather()方法直接請求網路資料?你考慮下這種情況:現在代碼中的網路請求是使用Volley架構來實現的,如果哪天老闆非要你使用Afinal架構實現網路請求,你怎麼解決問題?難道是修改 getWeather()方法的實現? no no no,這樣修改不僅破壞了以前的代碼,而且還不利於維護, 考慮到以後代碼的擴充和維護性,我們選擇設計介面的方式來解決著一個問題,我們實現另外一個WeatherModelWithAfinalImpl類,繼承自WeatherModel,重寫裡面的方法,這樣不僅保留了以前的WeatherModelImpl類請求網路方式,還增加了WeatherModelWithAfinalImpl類的請求方式。Activity調用代碼無需要任何修改。
MVC使用總結
轉載註明出處:http://blog.csdn.net/feiduclear_up/article/details/46363207 廢墟的樹的部落格
源碼樣本下載