Android为TV端助力 转载:android MVC设计模式

Controller控制器

import android.app.Dialog;  

import android.app.ProgressDialog;  

  1. import android.os.Bundle;  
  2. import android.support.v7.app.ActionBarActivity;  
  3. import android.view.View;  
  4. import android.widget.EditText;  
  5. import android.widget.TextView;  
  6. import android.widget.Toast;  
  7. import com.xjp.androidmvcdemo.R;  
  8. import com.xjp.androidmvcdemo.entity.Weather;  
  9. import com.xjp.androidmvcdemo.entity.WeatherInfo;  
  10. import com.xjp.androidmvcdemo.model.OnWeatherListener;  
  11. import com.xjp.androidmvcdemo.model.WeatherModel;  
  12. import com.xjp.androidmvcdemo.model.WeatherModelImpl;  
  13. public class MainActivity extends ActionBarActivity implements OnWeatherListener, View.OnClickListener {  
  14.     private WeatherModel weatherModel;  
  15.     private Dialog loadingDialog;  
  16.     private EditText cityNOInput;  
  17.     private TextView city;  
  18.     private TextView cityNO;  
  19.     private TextView temp;  
  20.     private TextView wd;  
  21.     private TextView ws;  
  22.     private TextView sd;  
  23.     private TextView wse;  
  24.     private TextView time;  
  25.     private TextView njd;  
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.         weatherModel = new WeatherModelImpl();  
  31.         initView();  
  32.     }  
  33.     /** 
  34.      * 初始化View 
  35.      */  
  36.     private void initView() {  
  37.         cityNOInput = findView(R.id.et_city_no);  
  38.         city = findView(R.id.tv_city);  
  39.         cityNO = findView(R.id.tv_city_no);  
  40.         temp = findView(R.id.tv_temp);  
  41.         wd = findView(R.id.tv_WD);  
  42.         ws = findView(R.id.tv_WS);  
  43.         sd = findView(R.id.tv_SD);  
  44.         wse = findView(R.id.tv_WSE);  
  45.         time = findView(R.id.tv_time);  
  46.         njd = findView(R.id.tv_njd);  
  47.         findView(R.id.btn_go).setOnClickListener(this);  
  48.         loadingDialog = new ProgressDialog(this);  
  49.         loadingDialog.setTitle(加载天气中...);  
  50.     }  
  51.     /** 
  52.      * 显示结果 
  53.      * 
  54.      * @param weather 
  55.      */  
  56.     public void displayResult(Weather weather) {  
  57.         WeatherInfo weatherInfo = weather.getWeatherinfo();  
  58.         city.setText(weatherInfo.getCity());  
  59.         cityNO.setText(weatherInfo.getCityid());  
  60.         temp.setText(weatherInfo.getTemp());  
  61.         wd.setText(weatherInfo.getWD());  
  62.         ws.setText(weatherInfo.getWS());  
  63.         sd.setText(weatherInfo.getSD());  
  64.         wse.setText(weatherInfo.getWSE());  
  65.         time.setText(weatherInfo.getTime());  
  66.         njd.setText(weatherInfo.getNjd());  
  67.     }  
  68.     /** 
  69.      * 隐藏进度对话框 
  70.      */  
  71.     public void hideLoadingDialog() {  
  72.         loadingDialog.dismiss();  
  73.     }  
  74.     @Override  
  75.     public void onClick(View v) {  
  76.         switch (v.getId()) {  
  77.             case R.id.btn_go:  
  78.                 loadingDialog.show();  
  79.                 weatherModel.getWeather(cityNOInput.getText().toString().trim(), this);  
  80.                 break;  
  81.         }  
  82.     }  
  83.     @Override  
  84.     public void onSuccess(Weather weather) {  
  85.         hideLoadingDialog();  
  86.         displayResult(weather);  
  87.     }  
  88.     @Override  
  89.     public void onError() {  
  90.         hideLoadingDialog();  
  91.         Toast.makeText(this, 获取天气信息失败, Toast.LENGTH_SHORT).show();  
  92.     }  
  93.     private <t extends="" view=""> T findView(int id) {  
  94.         return (T) findViewById(id);  
  95.     }  
  96. }  
从上面代码可以看到,Activity持有了WeatherModel模型的对象,当用户有点击Button交互的时候,Activity作为Controller控制层读取View视图层EditTextView的数据,然后向Model模型发起数据请求,也就是调用WeatherModel对象的方法 getWeathre()方法。当Model模型处理数据结束后,通过接口OnWeatherListener通知View视图层数据处理完毕,View视图层该更新界面UI了。然后View视图层调用displayResult()方法更新UI。至此,整个MVC框架流程就在Activity中体现出来了。

Model模型

来看看WeatherModelImpl代码实现

  1. package com.xjp.androidmvcdemo.model;  
  2.    
  3. /** 
  4.  * Description:请求网络数据接口 
  5.  * User: xjp 
  6.  * Date: 2015/6/3 
  7.  * Time: 15:40 
  8.  */  
  9.    
  10. public interface WeatherModel {  
  11.     void getWeather(String cityNumber, OnWeatherListener listener);  
  12. }  
  13.    
  14. ................  
  15.    
  16.    
  17. package com.xjp.androidmvcdemo.model;  
  18.    
  19. import com.android.volley.Response;  
  20. import com.android.volley.VolleyError;  
  21. import com.xjp.androidmvcdemo.entity.Weather;  
  22. import com.xjp.androidmvcdemo.volley.VolleyRequest;  
  23.    
  24. /** 
  25.  * Description:从网络获取天气信息接口实现 
  26.  * User: xjp 
  27.  * Date: 2015/6/3 
  28.  * Time: 15:40 
  29.  */  
  30.    
  31. public class WeatherModelImpl implements WeatherModel {  
  32.    
  33.     @Override  
  34.     public void getWeather(String cityNumber, final OnWeatherListener listener) {  
  35.    
  36.         /*数据层操作*/  
  37.         VolleyRequest.newInstance().newGsonRequest(http://www.weather.com.cn/data/sk/ + cityNumber + .html,  
  38.                 Weather.class, new Response.Listener<weather>() {  
  39.                     @Override  
  40.                     public void onResponse(Weather weather) {  
  41.                         if (weather != null) {  
  42.                             listener.onSuccess(weather);  
  43.                         } else {  
  44.                             listener.onError();  
  45.                         }  
  46.                     }  
  47.                 }, new Response.ErrorListener() {  
  48.                     @Override  
  49.                     public void onErrorResponse(VolleyError error) {  
  50.                         listener.onError();  
  51.                     }  
  52.                 });  
  53.     }  
  54. }  

以上代码看出,这里设计了一个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使用总结

利用MVC设计模式,使得这个天气预报小项目有了很好的可扩展和维护性,当需要改变UI显示的时候,无需修改Contronller(控制器)Activity的代码和Model(模型)WeatherModel模型中的业务逻辑代码,很好的将业务逻辑和界面显示分离。

Android项目中,业务逻辑,数据处理等担任了Model(模型)角色,XML界面显示等担任了View(视图)角色,Activity担任了Contronller(控制器)角色。contronller(控制器)是一个中间桥梁的作用,通过接口通信来协同 View(视图)和Model(模型)工作,起到了两者之间的通信作用。

什么时候适合使用MVC设计模式?当然一个小的项目且无需频繁修改需求就不用MVC框架来设计了,那样反而觉得代码过度设计,代码臃肿。一般在大的项目中,且业务逻辑处理复杂,页面显示比较多,需要模块化设计的项目使用MVC就有足够的优势了。

4.在MVC模式中我们发现,其实控制器Activity主要是起到解耦作用,将View视图和Model模型分离,虽然Activity起到交互作用,但是找Activity中有很多关于视图UI的显示代码,因此View视图和Activity控制器并不是完全分离的,也就是说一部分View视图和Contronller控制器Activity是绑定在一个类中的。

MVC的优点:

(1)耦合性低。所谓耦合性就是模块代码之间的关联程度。利用MVC框架使得View(视图)层和Model(模型)层可以很好的分离,这样就达到了解耦的目的,所以耦合性低,减少模块代码之间的相互影响。

(2)可扩展性好。由于耦合性低,添加需求,扩展代码就可以减少修改之前的代码,降低bug的出现率。

(3)模块职责划分明确。主要划分层M,V,C三个模块,利于代码的维护。

 
原文地址:https://www.cnblogs.com/xiaoxiaing/p/6372876.html